一款簡單實用的原生PHP分頁類,分頁按鈕樣式簡潔美觀,頁碼多的時候顯示「...」,也是挺多網站用的效果php
核心分頁代碼mysql
include_once("config.php"); require_once('page.class.php'); //分頁類 $showrow = 10; //一頁顯示的行數 $curpage = empty($_GET['page']) ? 1 : $_GET['page']; //當前的頁,還應該處理非數字的狀況 $url = "?page={page}"; //分頁地址,若是有檢索條件 ="?page={page}&q=".$_GET['q'] //省略了連接mysql的代碼,測試時自行添加 $sql = "SELECT id,content,addtime FROM wishing_wall"; $total = mysql_num_rows(mysql_query($sql)); //記錄總條數 if (!empty($_GET['page']) && $total != 0 && $curpage > ceil($total / $showrow)) $curpage = ceil($total_rows / $showrow); //當前頁數大於最後頁數,取最後一頁 //獲取數據 $sql .= " LIMIT " . ($curpage - 1) * $showrow . ",$showrow;"; $query = mysql_query($sql);
分頁列表數據顯示sql
<ul class="dates"> <?php while ($row = mysql_fetch_array($query)) { ?> <li> <span><?php echo $row['addtime'] ?></span> <a target="_blank" href="http://"><?php echo $row['content'] ?></a> </li> <?php } ?> </ul>
顯示分頁按鈕測試
<div class="showPage"> <?php if ($total > $showrow) {//總記錄數大於每頁顯示數,顯示分頁 $page = new page($total, $showrow, $curpage, $url, 2); echo $page->myde_write(); } ?> </div>