程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> 自學php分頁原理小結

自學php分頁原理小結

編輯:PHP基礎知識
 

//定每頁顯示多少條
//count(*)查詢總條數

//計算總頁數ceil()

//判斷$_GET 並賦值當前頁數

//mysql查詢 limit (當前頁數-1)*每頁條數,每頁條數

//如果總條數不為空,進行循環mysql_fetch_array()

//$_SERVER[‘PHP_SELF’]
//上一頁?page=當前頁-1
//下一頁?page=當前頁+1


<?php

include("conn.php");

 

$perpagenum = 3;

$total = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM message"));

$Total = $total[0];

$Totalpage =ceil($Total/$perpagenum);

 

if(!isset($_GET['page'])||!intval($_GET['page'])||$_GET['page']>$Totalpage){

$page = 1;

}else{

$page = $_GET['page'];

}

$start = ($page-1)*$perpagenum;

$sql = "SELECT * FROM message ORDER BY id DESC LIMIT $start,$perpagenum";

$result = mysql_query($sql);

$contents = mysql_fetch_array($result);

?>

<table width="849" height="90">

<?php

if($total){

do{

$user = $contents['user'];

$contact = $contents['contact'];

$content = $contents['content'];

$date = $contents['date'];

?>

<tr bgcolor="#B3DBB5">

<td width="193" height="22">昵稱:<?php echo $user;?></td>

<td width="373"><?php echo $contact;?></td>

<td width="261">發表於:<?php echo $date;?></td>

</tr>

<tr bgcolor="#D8D8D8">

<td height="auto" colspan="3"><?php echo $content;?></td>

</tr>

<?php

}

while($contents=mysql_fetch_array($result));

 

$per = $page-1;

$next= $page+1;

echo "<center>共有".$Total."條留言,每頁".$perpagenum."條,共".$page."/".$Totalpage."頁。";

if($page!=1){

echo "<a href=".$_SERVER['PHP_SELF'].">首頁</a>".

"<a href=".$_SERVER['PHP_SELF']."?page=$per".">上一頁</a>";

}

if($page!=$Totalpage){

echo "<a href=".$_SERVER['PHP_SELF']."?page=$next".">下一頁</a>";

echo "<a href=".$_SERVER['PHP_SELF']."?page=$Totalpage".">尾頁</a>";

}

 

?>

<?php

}

?>

</table>

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