php mysql数据库 分页与搜索

时间:2023-03-10 02:55:44
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>