程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP分頁函數代碼(簡單實用型)

PHP分頁函數代碼(簡單實用型)

編輯:關於PHP編程

准備數據:

新建一個數據庫 test
執行下面的語句(新建一個表 test :id、sex、name 三個字段)

CREATE TABLE `test` (
`id` INT( 4 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`sex` INT( 1 ) NOT NULL ,
`name` VARCHAR( 20 ) NOT NULL ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_bin;

添加數據到 test 表,比如:
1 1 小強
2 0 小紅
3 0 小麗
4 1 小兵
5 1 張三
6 0 李四
7 0 吳欣

寫PHP語句(index.php):
復制代碼 代碼如下:
<?php
// Page分頁函數
$page = $_GET["page"];
function Page($rows,$page_size){
global $page,$select_from,$select_limit,$pagenav;
$page_count = ceil($rows/$page_size);
if($page <= 1 || $page == '') $page = 1;
if($page >= $page_count) $page = $page_count;
$select_limit = $page_size;
$select_from = ($page - 1) * $page_size.',';
$pre_page = ($page == 1)? 1 : $page - 1;
$next_page= ($page == $page_count)? $page_count : $page + 1 ;
$pagenav .= "第 $page/$page_count 頁 共 $rows 條記錄 ";
$pagenav .= "<a href='?page=1'>首頁</a> ";
$pagenav .= "<a href='?page=$pre_page'>前一頁</a> ";
$pagenav .= "<a href='?page=$next_page'>後一頁</a> ";
$pagenav .= "<a href='?page=$page_count'>末頁</a>";
$pagenav.=" 跳到<select name='topage' size='1' onchange='window.location=\"?page=\"+this.value'>\n";
for($i=1;$i<=$page_count;$i++){
if($i==$page) $pagenav.="<option value='$i' selected>$i</option>\n";
else $pagenav.="<option value='$i'>$i</option>\n";
}
} // Page分頁函數
// 使用示例
if (!$conn= mysql_connect("localhost", "root" ,"root")) die('數據庫選擇失敗!');
if (!mysql_select_db("test", $conn)) die('數據庫選擇失敗!');
mysql_query('set names GBK');
// 用Page函數計算出 $select_from 從哪條記錄開始檢索、$pagenav 輸出分頁導航
$rows = mysql_num_rows(mysql_query("select * from test"));
Page($rows,2);
$sql = "select * from test limit $select_from $select_limit";
$rst = mysql_query($sql);
while ($row = mysql_fetch_array($rst)){
echo "$row[id] - $row[sex] - $row[name] <hr />";
}
echo $pagenav;
?>

浏覽 index.php 頁面,如圖:

該說byebye了,確實好用!

PHP簡單分頁函數
寫了一個php簡單的分頁函數,數據庫調用也寫到裡面了 ,用的人可以自行刪減!
復制代碼 代碼如下:
function getask(){
$sql = "select * from cms_ask where ansower <> ' ' ";
//這裡要改成方法
$q_sq = mysql_query($sql);
$count = mysql_num_rows($q_sq);
$page_size = 8;
$page_current = isset($GLOBALS['page']) ? intval($GLOBALS['page']) : 1;
$page_count = ceil($count / $page_size);
$page_start = $page_current - 4;
$page_end = $page_current + 4;
if ($page_current < 5) {
$page_start = 1;
$page_end = 5;
}
if ($page_current > $page_count - 4) {
$page_start = $page_count - 8;
$page_end = $page_count;
}
if ($page_start < 1)
$page_start = 1;
if ($page_end > $page_count)
$page_end = $page_count;
$pagebar = "";
$sql = "select * from cms_ask where ansower <> ' ' order by id desc limit " . (($page_current - 1) * $page_size) . "," . $page_size;
$row=$this -> user -> getall("$sql");
foreach($row as $v){
echo '<dl><dt>問:'.$v["question"].'</dt><dd>答:'.$v["ansower"].date("Y-m-d H:i:s",$v["postTime"]).'</dd></dl>';

}

$pagebar .= "<div class=\"page\">";
$pagebar .= "<ol>";
if ($page_current != 1) {
$pagebar .= '<li><a href="javascript:get_comment(1)" class="sx">FIRST</a></li>';
}
for ($i = $page_start; $i <= $page_end; $i++) {
if ($i == $page_current) {
$pagebar .= "<li><span class=\"sort\">" . $i . "</span></li>";
} else {
$pagebar .= "<li><a href='javascript:get_comment(" . $i . ")'>" . $i . "</a></li>";
}
}

if ($page_current != $page_count) {
$pagebar .= "<li><a href='javascript:get_comment(" . $page_count . ")' class='sx'>END</a></li>";
}
$pagebar .= "</ol>";
$pagebar .= " </div>";
echo $pagebar;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved