php mysql資料庫 分頁與搜尋

來源:互聯網
上載者:User

標籤:


<?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資料庫 分頁與搜尋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.