php mysql数据库 分页与搜索

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php mysql数据库 分页与搜索相关的知识,希望对你有一定的参考价值。


<?php
/**
* Created by coder meng.
* User: coder meng
* Date: 2016/8/29 10:27
*/
header("Content-type:text/html;charset=utf-8");
include_once ‘config.php‘;

/*$sql="select * from article ";
$rs=mysql_query($sql);*/
$where=‘‘;
$url=‘‘;
$wherelist=array();
$urllist="";
if(!empty($_GET[‘author‘])){
$author=$_GET[‘author‘];
$wherelist[]="author like ‘%$author%‘";
$urllist[]="&author=$author";
}
if(!empty($_GET[‘title‘])){
$title=$_GET[‘title‘];
$wherelist[]="title like ‘%$title%‘";
$urllist[]="&title=$title";
}
if(!empty($_GET[‘content‘])){
$content=$_GET[‘content‘];
$wherelist[]="content like ‘%$content%‘";
$urllist[]="&content=$content";
}

if(count($wherelist)>0){
$where=‘ where ‘.implode(‘ and ‘,$wherelist);
$url=implode(‘‘,$urllist);
}

$sql2="select * from article $where ";
$rs=mysql_query($sql2);
//$sql1="select * from article $limit"; // (1-1)*10
// 2, 共多少页 = 总记录数/每页条数
$totlanums=mysql_num_rows($rs); // 101 符合的 13
// 1, 每页条数
$pagesize=2;
//共多少页
$maxpage=ceil($totlanums/$pagesize);//最大页码 也是总页数
$page=isset($_GET[‘page‘])?$_GET[‘page‘]:1;
if($page<1){
$page=1;
}
if($page>$maxpage){
$page=$maxpage;
}
$start=($page-1)*$pagesize;
$limit ="limit $start,$pagesize";
$sql3="select * from article $where $limit";
$rs3=mysql_query($sql3);
$arr=array();

//分页的原理

/*while($row = mysql_fetch_assoc($rs)){
$arr[] =$row;
}*/

/*$sql1="select * from article limit 10,10"; //(1-1)*10
$sql1="select * from article limit 20,10"; //(1-1)*10*/

?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div style="width: 600px;margin: 0 auto;">
<form action="pagelist.php" method="get">
<table border="1" width="600">
<tr>
<td>作者:<input type="text" name="author" value="<?php echo isset( $_GET[‘author‘])?$_GET[‘author‘]:‘‘;?>"></td>
<td>标题:<input type="text" name="title" value="<?php echo isset( $_GET[‘title‘])?$_GET[‘title‘]:‘‘;?>"></td>
<td>内容:<input type="text" name="content" value="<?php echo isset( $_GET[‘content‘])?$_GET[‘content‘]:‘‘;?>"></td>
<td><input type="submit" value="搜索"></td>
</tr>
</table>
</form>
<table border="1" width="600">
<tr>
<td>编号</td>
<td>作者</td>
<td>标题</td>
<td>内容</td>
<td>操作</td>
</tr>
<?php while($row = mysql_fetch_assoc($rs3)){ ?>
<tr>
<td><?php echo $row[‘id‘]?></td>
<td><?php echo $row[‘author‘]?></td>
<td><?php echo $row[‘title‘]?></td>
<td><?php echo $row[‘content‘]?></td>
<td>编辑|删除</td>
</tr>
<?php } ?>
<tr>
<td colspan="5" align="center">
<?php
echo "当前第{$page}页/{$maxpage},共{$totlanums}条记录";
if($page>1){
echo "<a href=‘pagelist.php?page=1$url‘>首页</a>";
echo "<a href=‘pagelist.php?page=".($page-1)."$url‘>上一页</a>";
}
if($page<$maxpage){
echo "<a href=‘pagelist.php?page=".($page+1)."$url‘>下一页</a>";
echo "<a href=‘pagelist.php?page=$maxpage{$url}‘>尾页</a>";
}
?>
</td>
</tr>
</table>
</div>

以上是关于php mysql数据库 分页与搜索的主要内容,如果未能解决你的问题,请参考以下文章

mysql进阶之细谈索引分页与慢日志

oracle 的分页与 mySQL'的分页转化

6.9PHP分页与更新数据

mysql之分页与慢日志以及表知识补充

物理分页与逻辑分页

从分页搜索链接到mysql单个记录