PHP搜索MYSQL数据库加分页浏览小结

时间:2023-11-24 13:19:20
PHP搜索加分页浏览小结:
1 分页后再做搜索
2 这里对于url的拼接,以及模糊查询,搜索时候的显示添加,SQL语句的拼接
3 对于页面传递过来的超级链接的变量,如果不存在就要设置,对于可能抛出异常的要加上@屏蔽错误
4 对于查询一般用一些关键字来获取
5 分页显示的limit条件要写好
6 搜索时候的where条件 搜索表单:
<!--- 搜索表单--->
<form action="list3.php" method="get">
标题:<input type="text" name="title" size="10" value="<?php echo @$_GET['title'];?>"/> 
关键字:<input type="text" name="keywords" size="10" value="<?php echo @$_GET['keywords'];?>"/> 
作者:<input type="text" name="author" size="10" value="<?php echo @$_GET['author'];?>"/> 
<input type="submit" value="搜索"/>
<input type="button" value="全部信息" onclick="window.location='list3.php'"/>
</form>
<!----------------> 分页封装
<?php
//=======================
//封装搜索的信息
//定义一个封装搜索条件的数组变量
$wherelist=array();
//定义一个封装搜索的url,用于放置到url后面作为参数
$urllist=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']}";
}
//组装搜索条件
//将数组合并成字符串用implode();
if(count($wherelist)>0){
$where=" where ".implode(" and ",$wherelist);
$url=@"&".implode("&",$urllist);
}
//echo @$where;
//echo @$url; //=======================
?> 3 分页处理
<?php
//=====插入分页处理代码=====
//1 定义分页的变量
$page=isset($_GET['page'])?$_GET['page']:1;//当前页数,默认为1
$pageSize=4; //页大小
$maxRows=""; //最大数据条数
$maxPages=""; //最大页数 //获取最大数据条数
@$sql="select count(*) from news {$where}";
$res=mysql_query($sql,$conn);
$maxRows=mysql_result($res,0,0);//定位从结果集中获取总数据的条数,就是获取第一个单元格中的值 //3 计算出最大页数
$maxPages=ceil($maxRows/$pageSize);//进一取整获取最大页数,7/3;
//4 判断页数是否越界,判断是否有效
if($page>$maxPages){
$page=$maxPages;
//判断是否超出了最大页
}
if($page<1){
$page=1;
}
//拼接$sql,限制每页显示的条数
$limit=" limit ".(($page-1)*$pageSize).",{$pageSize}";
//起始位置是当前页减1乘以每页显示的条数
//==========分页封装结束==============
@$sql="select * from news {$where} order by addtime desc {$limit}";
//limit 0,3表示从第一条记录到第三条记录
//将最新的新闻先显示出来
$result=@mysql_query($sql,$conn);?> 具体实例:新闻搜索和分页程序:
搜索和分页功能如下: list3.php 完整代码
<?php
header("content-Type:text/html;charset=utf-8");
?>
<?php
require("menu.php");
require("dbconfig.php");
?>
<title>新闻信息管理系统</title>
<script>
function dodel(id){
//判断是否要删除
if(confirm("确定要删除吗?")){
window.location="action.php?action=del&id="+id;
} } </script>
<center>
<h2>搜索和分页浏览新闻</h2>
<!--- 搜索表单--->
<form action="list3.php" method="get">
标题:<input type="text" name="title" size="10" value="<?php echo @$_GET['title'];?>"/> 
关键字:<input type="text" name="keywords" size="10" value="<?php echo @$_GET['keywords'];?>"/> 
作者:<input type="text" name="author" size="10" value="<?php echo @$_GET['author'];?>"/> 
<input type="submit" value="搜索"/>
<input type="button" value="全部信息" onclick="window.location='list3.php'"/>
</form>
<!---------------->
<table border="1" cellpadding="2" cellspacing="0">
<tr><th>新闻id号</th><th>新闻标题</th><th>发布者</th>
<th>关键字</th><th>添加时间</th><th>新闻内容</th><th>操作</th>
</tr>
<?php
//=======================
//封装搜索的信息
//定义一个封装搜索条件的数组变量
$wherelist=array();
//定义一个封装搜索的url,用于放置到url后面作为参数
$urllist=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']}";
}
//组装搜索条件
//将数组合并成字符串用implode();
if(count($wherelist)>0){
$where=" where ".implode(" and ",$wherelist);
$url=@"&".implode("&",$urllist);
}
//echo @$where;
//echo @$url; //=======================
?>
<?php
//=====插入分页处理代码=====
//1 定义分页的变量
$page=isset($_GET['page'])?$_GET['page']:1;//当前页数,默认为1
$pageSize=4; //页大小
$maxRows=""; //最大数据条数
$maxPages=""; //最大页数 //获取最大数据条数
@$sql="select count(*) from news {$where}";
$res=mysql_query($sql,$conn);
$maxRows=mysql_result($res,0,0);//定位从结果集中获取总数据的条数,就是获取第一个单元格中的值 //3 计算出最大页数
$maxPages=ceil($maxRows/$pageSize);//进一取整获取最大页数,7/3;
//4 判断页数是否越界,判断是否有效
if($page>$maxPages){
$page=$maxPages;
//判断是否超出了最大页
}
if($page<1){
$page=1;
}
//拼接$sql,限制每页显示的条数
$limit=" limit ".(($page-1)*$pageSize).",{$pageSize}";
//起始位置是当前页减1乘以每页显示的条数
//==========分页封装结束==============
@$sql="select * from news {$where} order by addtime desc {$limit}";
//limit 0,3表示从第一条记录到第三条记录
//将最新的新闻先显示出来
$result=@mysql_query($sql,$conn);
while($row=@mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['title']}</td>";
echo "<td>{$row['author']}</td>";
echo "<td>{$row['keywords']}</td>";
echo "<td>".date("Y-m-d H:i:s",$row['addtime'])."</td>";
echo "<td>{$row['content']}</td>";
echo "<td><a href='javascript:dodel({$row['id']})'>删除</a> | <a href='edit.php?id={$row['id']}'>修改 </a></td>";
echo "</tr>"; }
//释放结果集
@mysql_free_result($result);
mysql_close($conn);
?>
</table>
<?php
//显示当前页数值,上一页和下一页
echo "<br/>";
echo "当前页 {$page}/{$maxPages}页 共计:{$maxRows}条  ";?>
<a href="list3.php?page=1<?php echo @$url;?>">首页</a>  
<a href="list3.php?page=<?php echo ($page-1).@$url;?>">上一页</a>  
<!--这里用分割符号分离出来添加页数--->
<a href="list3.php?page=<?php echo ($page+1).@$url;?>">下一页</a>  
<a href="list3.php?page=<?php echo $maxPages.@$url;?>">尾页</a> </center>