温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何实现PHP搜索加分页

发布时间:2021-08-26 15:41:32 来源:亿速云 阅读:148 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“如何实现PHP搜索加分页”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何实现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搜索加分页”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI