程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php開發分頁實現代碼第1/3頁

php開發分頁實現代碼第1/3頁

編輯:PHP綜合

項目結構:

運行效果:

conn.php
復制代碼 代碼如下:
<?php
$conn = @ mysql_connect("localhost", "root", "") or die("數據庫鏈接錯誤");
mysql_select_db("form", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文編碼;
//替換空格,回車鍵
function htmtocode($content) {
$content = str_replace("\n", "<br>", str_replace(" ", " ", $content));
return $content;
}
?>

page.php
復制代碼 代碼如下:
1 <?php
2
3 function _PAGEFT($totle, $displaypg = 20, $url = '') {
4
5 global $page, $firstcount, $pagenav, $_SERVER;
6
7 $GLOBALS["displaypg"] = $displaypg;
8
9 if (!$page)
$page = 1;
if (!$url) {
$url = $_SERVER["REQUEST_URI"];
}
//URL分析:
$parse_url = parse_url($url);
$url_query = $parse_url["query"]; //單獨取出URL的查詢字串
if ($url_query) {
$url_query = ereg_replace("(^|&)page=$page", "", $url_query);
$url = str_replace($parse_url["query"], $url_query, $url);
if ($url_query)
$url .= "&page";
else
$url .= "page";
} else {
$url .= "?page";
}
$lastpg = ceil($totle / $displaypg); //最後頁,也是總頁數
$page = min($lastpg, $page);
$prepg = $page -1; //上一頁
$nextpg = ($page == $lastpg ? 0 : $page +1); //下一頁
$firstcount = ($page -1) * $displaypg;
//開始分頁導航條代碼:
$pagenav = "顯示第 <B>" . ($totle ? ($firstcount +1) : 0) . "</B>-<B>" . min($firstcount + $displaypg, $totle) . "</B> 條記錄,共 $totle 條記錄";
//如果只有一頁則跳出函數:
if ($lastpg <= 1)
return false;
$pagenav .= " <a href='$url=1'>首頁</a> ";
if ($prepg)
$pagenav .= " <a href='$url=$prepg'>前頁</a> ";
else
$pagenav .= " 前頁 ";
if ($nextpg)
$pagenav .= " <a href='$url=$nextpg'>後頁</a> ";
else
$pagenav .= " 後頁 ";
$pagenav .= " <a href='$url=$lastpg'>尾頁</a> ";
//下拉跳轉列表,循環列出所有頁碼:
$pagenav .= " 到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for ($i = 1; $i <= $lastpg; $i++) {
if ($i == $page)
$pagenav .= "<option value='$i' selected>$i</option>\n";
else
$pagenav .= "<option value='$i'>$i</option>\n";
}
$pagenav .= "</select> 頁,共 $lastpg 頁";
}
include("conn.php");
$result=mysql_query("SELECT * FROM `test`");
$total=mysql_num_rows($result);
//調用pageft(),每頁顯示10條信息(使用默認的20時,可以省略此參數),使用本頁URL(默認,所以省略掉)。
_PAGEFT($total,5);
echo $pagenav;
$result=mysql_query("SELECT * FROM `test` limit $firstcount,$displaypg ");
while($row=mysql_fetch_array($result)){
echo "<hr><b>".$row[name]." | ".$row[sex];
}
?>

list.php
[code]
<?php
include("conn.php");
$pagesize=5;
$url=$_SERVER["REQUEST_URI"];
$url=parse_url($url);
$url=$url[path];
$numq=mysql_query("SELECT * FROM `test`");
$num = mysql_num_rows($numq);
if($_GET 當前1/3頁 123下一頁閱讀全文
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved