php分页代码

这是一款简单实用的php分页代码,同时也很好的利用的类的构造函数来实例分页的初始化,好了下面我们来看看这款代码如何吧,代码如下:

  1. class page extends mysql
  2. {
  3. public $page;
  4. public $page_size;
  5. private $table;
  6. private $condition;
  7. private $limit;
  8. private $href;
  9. private $action_value;
  10. private $url_value;
  11. //分页初始化
  12. function __construct($page,$page_size,$table,$condition,$limit,$href,$action_value,$url_value)
  13. {
  14. $this->page=$page;
  15. $this->page_size=$page_size;
  16. $this->table=$table;
  17. $this->condition=$condition;
  18. $this->limit=$limit;
  19. $this->href=$href;
  20. $this->action_value=$action_value;
  21. $this->url_value=$url_value;
  22. $this->get_page();
  23. }
  24. /**
  25. * get the page's value判断当前在第几页
  26. */
  27. function get_page()
  28. {
  29. if($this->page=="")
  30. {
  31. $this->page=1;
  32. }
  33. return $this->page;
  34. }
  35. /**
  36. * page main code //调用转到指定的分页代码号。
  37. */
  38. function go_page()
  39. {
  40. if($this->page!="")
  41. {
  42. $reslut=mysql::fn_select($this->table,'count(*) as `total`',$this->condition,'',$this->limit);
  43. $rs=mysql::fetch_array($reslut);
  44. $message_count=$rs['total']; //get the messages's count number
  45. $page_count=ceil($message_count/$this->page_size); //get the page's count number
  46. $offset=($this->page-1)*$this->page_size; //get the first value of sql's limit
  47. if($message_count<=$this->page_size)
  48. {
  49. $page_code=array("","","","");
  50. }
  51. else if($this->page==1)
  52. {
  53. $page_code=array("",
  54. "",
  55. "<a name=code href=".$this->href."?page=".($this->page+1)."&action=".$this->action_value."&".$this->url_value.">下一页</a>",
  56. "<a name=code href=".$this->href."?page=".$page_count."&action=".$this->action_value."&".$this->url_value.">尾页</a>");
  57. }
  58. else if($this->page==$page_count)
  59. {
  60. $page_code=array("<a name='code' href=".$this->href."?page=1&action=".$this->action_value."&".$this->url_value.">首页</a>",
  61. "<a name='code' href=".$this->href."?page=".($this->page-1)."&action=".$this->action_value."&".$this->url_value.">上一页</a>",
  62. "",
  63. "");
  64. }
  65. else
  66. {
  67. $page_code=array("<a name='code' href=".$this->href."?page=1&action=".$this->action_value."&".$this->url_value.">首页</a>",
  68. "<a name='code' href=".$this->href."?page=".($this->page-1)."&action=".$this->action_value."&".$this->url_value.">上一页</a>",
  69. "<a name='code' href=".$this->href."?page=".($this->page+1)."&action=".$this->action_value."&".$this->url_value.">下一页</a>",
  70. "<a name='code' href=".$this->href."?page=".$page_count."&action=".$this->action_value."&".$this->url_value.">尾页</a>");
  71. }
  72. $page_info=array(
  73. "message_count"=>$message_count,
  74. "page_count"=>$page_count,
  75. "offset"=>$offset,
  76. "page_size"=>$this->page_size,
  77. "page_now"=>$this->page,
  78. "page_first"=>$page_code[0],
  79. "page_up"=>$page_code[1],
  80. "page_next"=>$page_code[2],
  81. "page_one_last"=>$page_code[3]
  82. );//开源代码phpfensi.com
  83. return $page_info;
  84. }
  85. }
  86. }

php分页代码调用方法,代码如下:

  1. $page = new page('',10,'`cy0871_users_info`','index.php',1,'','','user);
  2. print_r ($page->go_page());
  3. $page_info = $page->go_page();
  4. echo $page_info['page']."<br/>";
  5. echo $page_info['page_size'];