PHP簡易分頁類

來源:互聯網
上載者:User

這是一個簡單易用的分頁類。只需在你原有的程式中加兩句、改一句就可以了
先貼代碼
paging.php


PHP code

  
<?phpclass Paging {  public static $count = 0;  public static $size = 0;  public static $page = 0;  static function prepare($sql, $pagesize=10) {    $page = isset($_GET['page']) ? $_GET['page'] : 1;    $pageon = ($page - 1) * $pagesize;    $sql = preg_replace('/select\s/i', '$0SQL_CALC_FOUND_ROWS ', $sql) . " limit $pageon, $pagesize";    $rs = mysql_query($sql);    $p = mysql_query('SELECT FOUND_ROWS()');    list(self::$count) = mysql_fetch_row($p);    self::$size = $pagesize;    self::$page = $page;    return $rs;  }  static function bar($tpl='') {    if(!$tpl) $tpl = '<a href=?reset>首頁</a> <a href=?prve>上一頁</a> <a href=?next>下一頁</a> <a href=?end>尾頁</a>';    $count = ceil(self::$count / self::$size);    $page = self::$page;    unset($_GET['page']);    $d = array(      'reset' => 1,      'prve' => $page > 1 ? $page - 1 : 1,      'next' => $page < $count ? $page + 1 : $count,      'end' => $count,    );    foreach($d as $k=>$v) {      $_GET['page'] = $v;      $tpl = str_replace($k, http_build_query($_GET), $tpl);    }    echo $tpl;  }}

通常你都有類似這樣的語句 $sql ="....."; $rs = mysql_query($sql); 或 $rs = mysql_query("select ...."); 你只需改作 include 'paging.php'; $rs = paging::prepare($sql, 每頁行數); 在需要出現分頁條的地方寫入 paging::bar(); 就可以了,非常簡單! 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.