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="查看全部" οnclick="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>

  

转载于:https://www.cnblogs.com/phpmaster/p/5942297.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值