實(shí)現(xiàn)PHP搜索加分頁(yè)
分頁(yè)顯示是瀏覽大量數(shù)據(jù)的一種方法。對(duì)于初學(xué)者來(lái)說(shuō)常常對(duì)這個(gè)問(wèn)題摸不著頭緒,因此特地撰寫(xiě)此文對(duì)這個(gè)問(wèn)題進(jìn)行詳細(xì)的講解,力求讓看完這篇文章的朋友在看完以后對(duì)于分頁(yè)顯示的原理和實(shí)現(xiàn)方法有所了解。
所有示例代碼均使用php編寫(xiě)。
所謂分頁(yè)顯示,也就是將數(shù)據(jù)庫(kù)中的結(jié)果集人為的分成一段一段的來(lái)顯示。
請(qǐng)?jiān)敿?xì)閱讀以下代碼,自己調(diào)試運(yùn)行一次,最好把它修改一次,加上自己的功能。
$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);
}
//分頁(yè)的實(shí)現(xiàn)原理
//1.獲取數(shù)據(jù)表中總記錄數(shù)
$sql="select count(*) from news $where ";
$result=mysql_query($sql);
$totalnum=mysql_num_rows($result);
//每頁(yè)顯示條數(shù)
$pagesize=5;
//總共有幾頁(yè)
$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">
標(biāo)題:<input type="text" name="title" value="<?php echo $_GET['title']?>" size="8">
關(guān)鍵字<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>編號(hào)</td>
<td>標(biāo)題</td>
<td>關(guān)鍵字</td>
<td>作者</td>
<td>日期</td>
<td>內(nèi)容</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 " 當(dāng)前{$page}/{$maxpage}頁(yè) 共{$totalnum}條";
echo " <a href='searchpage.php?page=1{$url}'>首頁(yè)</a> ";
echo "<a href='searchpage.php?page=".($page-1)."{$url}'>上一頁(yè)</a>";
echo "<a href='searchpage.php?page=".($page+1)."{$url}'>下一頁(yè)</a>";
echo " <a href='searchpage.php?page={$maxpage}{$url}'>尾頁(yè)</a> ";
?>
</td>
</tr>
</table>
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- Thinkphp搜索時(shí)首頁(yè)分頁(yè)和搜索頁(yè)保持條件分頁(yè)的方法
- .net搜索查詢(xún)并實(shí)現(xiàn)分頁(yè)實(shí)例
- jquery+json實(shí)現(xiàn)的搜索加分頁(yè)效果
- ThinkPHP讓分頁(yè)保持搜索狀態(tài)的方法
- Ajax搜索結(jié)果頁(yè)面下方的分頁(yè)按鈕的生成
- BootStrap Table 分頁(yè)后重新搜索問(wèn)題的解決辦法
- 使用Java的Lucene搜索工具對(duì)檢索結(jié)果進(jìn)行分組和分頁(yè)
- ajax結(jié)合豆瓣搜索結(jié)果進(jìn)行分頁(yè)完整代碼
相關(guān)文章
國(guó)產(chǎn)PHP開(kāi)發(fā)框架myqee新手快速入門(mén)教程
這篇文章主要介紹了國(guó)產(chǎn)PHP開(kāi)發(fā)框架myqee新手快速入門(mén)教程,myqee中文名稱(chēng)邁啟PHP框架,有比較多的高級(jí)開(kāi)發(fā)特性,需要的朋友可以參考下2014-07-07
php無(wú)法連接mysql數(shù)據(jù)庫(kù)的正確解決方法
這篇文章主要為大家詳細(xì)介紹了php無(wú)法連接mysql數(shù)據(jù)庫(kù)的正確解決方法,感興趣的小伙伴們可以參考一下2016-07-07
一個(gè)PHP并發(fā)訪問(wèn)實(shí)例代碼
php其實(shí)也可以執(zhí)行多任務(wù)或并發(fā)訪問(wèn),下面就是網(wǎng)上找來(lái)的一個(gè)關(guān)于PHP并發(fā)訪問(wèn)的例子2012-09-09
IP138 IP地址查詢(xún)小偷實(shí)現(xiàn)代碼
下面的代碼是獲取ip138的數(shù)據(jù),并顯示。2010-02-02
PHP的new static和new self的區(qū)別與使用
這篇文章主要介紹了PHP的new static和new self的區(qū)別與使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Symfony2創(chuàng)建基于域名的路由相關(guān)示例
這篇文章主要介紹了Symfony2創(chuàng)建基于域名的路由,結(jié)合實(shí)例形式分析了Symfony2路由的創(chuàng)建技巧,需要的朋友可以參考下2016-11-11

