Home  >  Article  >  Backend Development  >  Analysis of examples of implementing PHP search and paging

Analysis of examples of implementing PHP search and paging

墨辰丷
墨辰丷Original
2018-06-01 09:50:211621browse

This article implements PHP search and paging, which solves the paging problem of a large number of browsers. It strives to let friends who read this article have an understanding of the principles and implementation methods of paging display after reading this article.

Paginated display is a way to browse large amounts of data. Beginners are often confused about this issue, so I specially wrote this article to explain this issue in detail, and strive to let friends who read this article understand the principles and implementation methods of paging display after reading this article. .

All sample codes are written in PHP.

The so-called paging display means that the result set in the database is artificially divided into segments for display.

Please read the following code in detail, debug and run it yourself, it is best to modify it once and add your own functions.

$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[&#39;title&#39;]?>" size="8">
关键字<input type="text" name="keywords" value="<?php echo $_GET[&#39;keywords&#39;]?>" size="8">
作者:<input type="text" name="author" value="<?php echo $_GET[&#39;author&#39;]?>" size="8">
 <input type="button" value="查看全部" onclick="window.location=&#39;searchpage.php&#39;">
 <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[&#39;id&#39;] ?></td>
 <td><?php echo $row[&#39;title&#39;] ?></td>
 <td><?php echo $row[&#39;keywords&#39;] ?></td>
 <td><?php echo $row[&#39;author&#39;] ?></td>
 <td><?php echo date("Y-m-d H:i:s",$row[&#39;addtime&#39;]) ?></td>
 <td><?php echo $row[&#39;content&#39;] ?></td>
 </tr>
<?php }?>
<tr>
 <td colspan="6">
<?php
 echo " 当前{$page}/{$maxpage}页 共{$totalnum}条";
echo " <a href=&#39;searchpage.php?page=1{$url}&#39;>首页</a> ";
echo "<a href=&#39;searchpage.php?page=".($page-1)."{$url}&#39;>上一页</a>";
echo "<a href=&#39;searchpage.php?page=".($page+1)."{$url}&#39;>下一页</a>";
echo " <a href=&#39;searchpage.php?page={$maxpage}{$url}&#39;>尾页</a> ";
 
?>
</td>
 </tr>
</table>

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

think method of executing transactions in the PHP framework

PHP MySQL uses mysql_num_rows to implement fuzzy query book information function

php curl method to get json object and convert it into array array

The above is the detailed content of Analysis of examples of implementing PHP search and paging. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn