分頁顯示是瀏覽大量數據的一種方法。對于初學者來說常常對這個問題摸不著頭緒,因此特地撰寫此文對這個問題進行詳細的講解,力求讓看完這篇文章的朋友在看完以后對于分頁顯示的原理和實現方法有所了解。
所有示例代碼均使用php編寫。
所謂分頁顯示,也就是將數據庫中的結果集人為的分成一段一段的來顯示。
請詳細閱讀以下代碼,自己調試運行一次,最好把它修改一次,加上自己的功能。
$wherelist=array();$urlist=array();if(!empty($_GET['title'])){$wherelist[]=" title like '%".$_GET['title']."%'";$urllist[]="title=".$_GET['title']; }if(!empty($_GET['keywords'])){$wherelist[]=" keywords like '%".$_GET['keywords']."%'";$urllist[]="keywords=".$_GET['keywords'];}if(!empty($_GET['author'])){$wherelist[]=" author like '%".$_GET['author']."%'";$urllist[]="author=".$_GET['author'];}$where="";if(count($wherelist)>0){$where=" where ".implode(' and ',$wherelist);$url='&'.implode('&',$urllist);}//分頁的實現原理//1.獲取數據表中總記錄數$sql="select count(*) from news $where ";$result=mysql_query($sql);$totalnum=mysql_num_rows($result);//每頁顯示條數$pagesize=5;//總共有幾頁$maxpage=ceil($totalnum/$pagesize);$page=isset($_GET['page'])?$_GET['page']:1;if($page <1){$page=1;}if($page>$maxpage){$page=$maxpage;}$limit=" limit ".($page-1)*$pagesize.",$pagesize";$sql1="select * from news {$where} {$limit}"; //$sql1="select * from news {$where} {$limit}";$res=mysql_query($sql1); ?><form action="searchpage.php" method="get">標題:<input type="text" name="title" value="<?php echo $_GET['title']?>" size="8">關鍵字<input type="text" name="keywords" value="<?php echo $_GET['keywords']?>" size="8">作者:<input type="text" name="author" value="<?php echo $_GET['author']?>" size="8"> <input type="button" value="查看全部" onclick="window.location='searchpage.php'"> <input type="submit" value="搜索"></form> <table border="1" width="1000" align="center"> <tr> <td>編號</td> <td>標題</td> <td>關鍵字</td> <td>作者</td> <td>日期</td> <td>內容</td> </tr><?php while($row= mysql_fetch_assoc($res)){?><tr> <td><?php echo $row['id'] ?></td> <td><?php echo $row['title'] ?></td> <td><?php echo $row['keywords'] ?></td> <td><?php echo $row['author'] ?></td> <td><?php echo date("Y-m-d H:i:s",$row['addtime']) ?></td> <td><?php echo $row['content'] ?></td> </tr><?php }?><tr> <td colspan="6"><?php echo " 當前{$page}/{$maxpage}頁 共{$totalnum}條";echo " <a href='searchpage.php?page=1{$url}'>首頁</a> ";echo "<a href='searchpage.php?page=".($page-1)."{$url}'>上一頁</a>";echo "<a href='searchpage.php?page=".($page+1)."{$url}'>下一頁</a>";echo " <a href='searchpage.php?page={$maxpage}{$url}'>尾頁</a> "; ?></td> </tr></table>希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選