php分頁代碼

來源:互聯網
上載者:User
 代碼如下 複製代碼

 class page extends mysql
 {
  public $page;
  public $page_size;
  private $table;
  private $condition;
  private $limit;
  private $href;
  private $action_value;
  private $url_value;
 //分頁初始化

  function __construct($page,$page_size,$table,$condition,$limit,$href,$action_value,$url_value)
  {
   $this->page=$page;
   $this->page_size=$page_size;
   $this->table=$table;
   $this->condition=$condition;
   $this->limit=$limit;
   $this->href=$href;
   $this->action_value=$action_value;
   $this->url_value=$url_value;
   $this->get_page();
  }

 /**
  * get the page's value判斷當前在第幾頁
  */
  function get_page()
  {
   if($this->page=="")
   {
    $this->page=1;
   }

   return $this->page;
  }

 /**
  * page main code //調用轉到指定的分頁代碼號。
  */
  function go_page()
  {
  if($this->page!="")
  {
   $reslut=mysql::fn_select($this->table,'count(*) as `total`',$this->condition,'',$this->limit);
   $rs=mysql::fetch_array($reslut);
   $message_count=$rs['total'];  //get the messages's count number
   $page_count=ceil($message_count/$this->page_size);  //get the page's count number
   $offset=($this->page-1)*$this->page_size;  //get the first value of sql's limit

   if($message_count<=$this->page_size)
   {
    $page_code=array("","","","");
   }
   else if($this->page==1)
   {
    $page_code=array("",
        "",
        "<a name=code href=".$this->href."?page=".($this->page+1)."&action=".$this->action_value."&".$this->url_value.">下一頁</a>",
        "<a name=code href=".$this->href."?page=".$page_count."&action=".$this->action_value."&".$this->url_value.">尾頁</a>");
   }
   else if($this->page==$page_count)
   {
       $page_code=array("<a name='code' href=".$this->href."?page=1&action=".$this->action_value."&".$this->url_value.">首頁</a>",
        "<a name='code' href=".$this->href."?page=".($this->page-1)."&action=".$this->action_value."&".$this->url_value.">上一頁</a>",
        "",
        "");
   }
   else
   {
    $page_code=array("<a name='code' href=".$this->href."?page=1&action=".$this->action_value."&".$this->url_value.">首頁</a>",
        "<a name='code' href=".$this->href."?page=".($this->page-1)."&action=".$this->action_value."&".$this->url_value.">上一頁</a>",
        "<a name='code' href=".$this->href."?page=".($this->page+1)."&action=".$this->action_value."&".$this->url_value.">下一頁</a>",
        "<a name='code' href=".$this->href."?page=".$page_count."&action=".$this->action_value."&".$this->url_value.">尾頁</a>");
   }

   $page_info=array(
       "message_count"=>$message_count,
       "page_count"=>$page_count,
       "offset"=>$offset,
       "page_size"=>$this->page_size,
       "page_now"=>$this->page,
       "page_first"=>$page_code[0],
       "page_up"=>$page_code[1],
       "page_next"=>$page_code[2],
       "page_one_last"=>$page_code[3]
       );

   return $page_info;
  }
  }
 }


//php分頁代碼調用方法

 代碼如下 複製代碼

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.