php mysql 分页函数

一款超简单的php mysql 分页,也是很实例的一款函数,他可以自动获取用户增加的参数,而不需要用户来增加,很自能化的一款自能分页程序,代码如下:

  1. +-----------------------------------------------------------------------
  2. | 文件概要:分页显示函数
  3. | 文件名称:page.php
  4. +-----------------------------------------------------------------------
  5. */
  6. function _pageft($totle, $displaypg = 20, $url = '') {
  7. global $page, $firstcount, $pagenav, $_server;
  8. $globals["displaypg"] = $displaypg;
  9. if (!$page)
  10. $page = 1;
  11. if (!$url) {//开源代码phpfensi.com
  12. $url = $_server["request_uri"];
  13. }
  14. //url分析:
  15. $parse_url = parse_url($url);
  16. $url_query = $parse_url["query"]; //单独取出url的查询字串
  17. if ($url_query) {
  18. $url_query = preg_replace("/(^|&)page=$page/", "", $url_query);
  19. $url = str_replace($parse_url["query"], $url_query, $url);
  20. if ($url_query)
  21. $url .= "&page";
  22. else
  23. $url .= "page";
  24. } else {
  25. $url .= "?page";
  26. }
  27. $lastpg = ceil($totle / $displaypg); //最后页,也是总页数
  28. $page = min($lastpg, $page);
  29. $prepg = $page -1; //上一页
  30. $nextpg = ($page == $lastpg ? 0 : $page +1); //下一页
  31. $firstcount = ($page -1) * $displaypg;
  32. //开始分页导航条代码:
  33. $pagenav = "显示第 <b>" . ($totle ? ($firstcount +1) : 0) . "</b>-<b>" . min($firstcount + $displaypg, $totle) . "</b> 条记录&nbsp;&nbsp;共 <font color='red'>$totle</font> 条留言";
  34. //如果只有一页则跳出函数:
  35. if ($lastpg <= 1)
  36. return false;
  37. $pagenav .= " <a href='$url=1'>首页</a> ";
  38. if ($prepg)
  39. $pagenav .= " <a href='$url=$prepg'>前页</a> ";
  40. else
  41. $pagenav .= " 前页 ";
  42. if ($nextpg)
  43. $pagenav .= " <a href='$url=$nextpg'>后页</a> ";
  44. else
  45. $pagenav .= " 后页 ";
  46. $pagenav .= " <a href='$url=$lastpg'>尾页</a> ";
  47. //下拉跳转列表,循环列出所有页码:
  48. $pagenav .= " 到第 <select class='border:1px solid #98ccfa;' name='topage' size='1' onchange='window.location="$url="+this.value'>n";
  49. for ($i = 1; $i <= $lastpg; $i++) {
  50. if ($i == $page)
  51. $pagenav .= "<option value='$i' selected>$i</option>n";
  52. else
  53. $pagenav .= "<option value='$i'>$i</option>n";
  54. }
  55. $pagenav .= "</select> 页&nbsp;&nbsp;&nbsp;共 <font color='red'>$lastpg</font> 页";
  56. }
  57. ?>