程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> php分頁例子實現讀取mysql數據分頁顯示

php分頁例子實現讀取mysql數據分頁顯示

編輯:PHP基礎知識
 

以下代碼是PHP分頁案例,測試通過,主要是PHP+mysql實現分頁,代碼來處百度空間,有興趣看的話可以了解一下PHP是如何分頁的?

<?php
$link = mysql_connect("localhost","root", "2855") //連接數據庫
or die("連接不上服務器:".mysql_error());
mysql_select_db("aming");
$ittype=$_GET['ittype'];
if($ittype==""){
echo "請輸入查詢條件";
exit;
}
$PageSize = 4;//每頁顯示記錄數
$StartRow = 0; //開始顯示記錄的編號
//獲取需要顯示的頁數,由用戶提交
if(empty($_GET['PageNo'])){ //如果為空,則表示第1頁
if($StartRow == 0){
$PageNo = $StartRow + 1; //設定為1
}
}else{
$PageNo = $_GET['PageNo']; //獲得用戶提交的頁數
$StartRow = ($PageNo-1) * $PageSize; //獲得開始顯示的記錄編號 $StartRow = ($PageNo - 1) * $PageSize;
}

//因為顯示頁碼的數量是動態變化的
//假如總共有一百頁,則不可能同時顯示100個鏈接
//而是根據當前的頁數顯示一定數量的頁面鏈接
//設置顯示頁碼的初始值
if($PageNo % $PageSize == 0){
$CounterStart = $PageNo - ($PageSize - 1);
}else{
$CounterStart = $PageNo - ($PageNo % $PageSize) + 1;
}

//顯示頁碼的最大值
$CounterEnd = $CounterStart + ($PageSize - 1);
?>

<html>
<head>
<title>分頁顯示記錄</title>
<link rel="stylesheet" href="include/style.css" type="text/css"></head>
<?php
/*$itname=$_POST['itname'];
if($itname ==""){
echo "請輸入查詢條件";
exit;
}*/

if($ittype)
$TRecord = mysql_query("select *from pc_it where ittype=$ittype");
$result = mysql_query("select *from pc_it where ittype=$ittype ORDER BY id DESC LIMIT $StartRow,$PageSize");

//獲取總記錄數
$RecordCount = mysql_num_rows($TRecord);

//獲取總頁數
$MaxPage = $RecordCount % $PageSize;
if($RecordCount % $PageSize == 0){
$MaxPage = $RecordCount / $PageSize;
}else{
$MaxPage = ceil($RecordCount / $PageSize);
}
?>
<body class="UsePageBg">
<table width="100%" border="0" class="InternalHeader">
<tr>
<td width="24%"><font size=4>分頁顯示記錄</font></td>
<td width="76%">
<font size=4>
<?php print "總共 $RecordCount 條記錄 - 當前頁: $PageNo of $MaxPage" ?>
</font>
</td>
</tr>
</table>
<br>
<table width="100%" border="0" class="NormalTableTwo">
<tr>
<td class="InternalHeader">型號</td>
<td class="InternalHeader" >參數</td>
<td class="InternalHeader" >價格</td>
</tr>
<?php
$i = 1;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$bil = $i + ($PageNo-1)*$PageSize; // $bil = $i + ($PageNo-1)*$PageSize;
?>
<tr>
<td class="NormalFieldTwo" ><?php echo $row[1] ?></td>
<td class="NormalFieldTwo" ><?php echo $row[3] ?></td>
<td class="NormalFieldTwo" ><?php echo $row[4] ?></td>
</tr>
<?php
$i++;
}?>
</table><br><table width="100%" border="0" class="InternalHeader">
<tr>
<td>

<div align="center">
<form action = search2.php method = GET >
<?php
echo "<font size=4>";
 

//顯示第一頁或者前一頁的鏈接
//如果當前頁不是第1頁,則顯示第一頁和前一頁的鏈接
if($PageNo != 1){
$PrevStart = $PageNo - 1;
print "<a href=search2.php?PageNo=1>首頁 </a>: ";
print "<a href=search2.php?PageNo=$PrevStart&ittype=$ittype>上一頁 </a>";
}
print " [ ";
$c=0;
//打印需要顯示的頁碼
for($c=$CounterStart;$c<=$CounterEnd;$c++){
if($c < $MaxPage){
if($c == $PageNo){
if($c % $PageSize == 0){
print "$c ";
}else{
print "$c,";
}
}else
if($c % $PageSize == 0){
echo "<a href=search2.php?ittype=$ittype&PageNo=$c>$c</a> ";
//echo "<a href=search2.php?PageNo=$c&ittype=$ittype>$c</a> ";
}else{
echo "<a href=search2.php?ittype=$ittype&PageNo=$c>$c,</a> ";
//echo "<a href=search2.php?PageNo=$cittype=$ittype>$c</a> ,";
} //END IF
}else{
if($PageNo == $MaxPage){
print "$c ";
break;
}else{
echo "<a href=search2.php?ittype=$ittype&PageNo=$c>$c</a> ";
//echo "<a href=search2.php?PageNo==$c&ittype=$ittype>$c</a> ";
break;
}//END IF
}//END IF
}//NEXT
echo "] ";
if($PageNo < $MaxPage){ //如果當前頁不是最後一頁,則顯示下一頁鏈接
$NextPage = $PageNo + 1;
echo "<a href=search2.php?PageNo=$NextPage&ittype=$ittype>下一頁</a>";
}

//同時如果當前頁補上最後一頁,要顯示最有一頁的鏈接
if($PageNo < $MaxPage){
$LastRec = $RecordCount % $PageSize;
if($LastRec == 0){
$LastStartRecord = $RecordCount - $PageSize;
}
else{
$LastStartRecord = $RecordCount - $LastRec;
}

print " : ";
echo "<a href=?PageNo=$MaxPage&ittype=$ittype>末頁</a>";
}

echo "<form action='search2.php' method='post'> ";
echo "轉到第<input type=text name='PageNo' size='2' value=".$PageNo.">頁";
echo "<input name=itname type=hidden value=".$ittype.">";
echo "<input type=submit name='Submit' value='Go'>";
echo "</form>";
echo "</font>";
 

?>
</form>
</div>
</td>
</tr>
</table>
<?php
mysql_free_result($result);
mysql_free_result($TRecord);
?>
</body>
</html>

 

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