查询第一页和查询总页数都是对的,但一点第二页where后面的条件就都丢失了。
这种问题一般有几种解决办法呢?
最好是post的方式,浏览器内页码不变最好。
请帮我找找用例啊,我学习一下呢。
谢谢!非常感谢!
pageClass.php 代码
<?php
class Page{
public $total;
public $pagesize;
public $limit;
public $page;
public $pagenum;
public $url;
public $bothnum;
public function __construct($_total, $_pagesize) {
$this->total = $_total;
$this->pagesize = $_pagesize;
$this->pagenum = ceil($this->total / $this->pagesize);
$this->page = $this->setPage();
$this->limit = "LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize";
$this->url = $this->setUrl();
$this->bothnum = 4;
}
private function setPage(){
if (!empty($_GET['page'])) {
if ($_GET['page'] > 0 && $_GET['page'] <= $this->pagenum) {
return $_GET['page'];
} else {
return 1;
}
} else {
return 1;
}
}
private function setUrl(){
$_url = $_SERVER["REQUEST_URI"];
$_par = parse_url($_url);
if (isset($_par['query'])) {
parse_str($_par['query'],$_query);
unset($_query['page']);
$_url = $_par['path'].'?'.http_build_query($_query).'&';
} else {
$_url = $_url . '?';
}
return $_url;
}
public function showPage(){
$_page = '';
$_page .= '<a href="'.$this->url.'page=1">First</a> ';
if ($this->page > $this->bothnum+1) {
$_page .= '<a href="'.$this->url.'page='.($this->page-1).'">Previous</a> ';
}
for ($i=$this->bothnum;$i>=1;$i--) {
if ($this->page-$i > 0) {
$_page .= '<a href="'.$this->url.'page='.($this->page-$i).'">'.($this->page-$i).'</a> ';
}
}
$_page .= '<span class="me">'.$this->page.'</span> ';
for ($i=1;$i<=$this->bothnum;$i++) {
if ($this->page+$i <= $this->pagenum) {
$_page .= '<a href="'.$this->url.'page='.($this->page+$i).'">'.($this->page+$i).'</a> ';
}
}
if ($this->page < $this->pagenum - $this->bothnum) {
$_page .= '<a href="'.$this->url.'page='.($this->page+1).'">Next</a> ';
}
$_page .= '<a href="'.$this->url.'page='.$this->pagenum.'">Last</a> ';
$_page .= '<span class="total">Total: '.$this->total.'</span> ';
return $_page;
}
}
search.php 代码
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once 'conn.php';
require_once 'PageClass.php';
$fields = array(
"name0" => "姓名",
"name1" => "年龄",
"name2" => "性别",
"name3" => "手机号",
"name4" => "地址",
);
$keywords = array();
foreach ($fields as $name => $field) {
if (isset($_POST[$name]) && !empty($_POST[$name])) {
$keywords[$field] = explode(" ", $_POST[$name]);
}
}
$query_rs = "1=1"; // 默认条件
if (!empty($keywords)) {
$query_rs_parts = array();
foreach($keywords as $field => $words) {
$query_rs_parts[] = $field . " LIKE '%" . implode("%' AND ". $field ." LIKE '%", $words) ."%'";
}
$query_rs = implode(" AND ", $query_rs_parts) ;
}
// 存储查询条件
session_start();
$_SESSION['query_rs'] = $query_rs;
// 查询总条数
$total_sql = "SELECT COUNT(*) FROM data_1 WHERE $query_rs";
$total_res = $dbh->query($total_sql);
$total = $total_res->fetchColumn();
// 实例化分页类
$page = new Page($total, 10, isset($_GET['page']) ? $_GET['page'] : 1);
// 查询
$name_sql = "SELECT * FROM data_1 WHERE $query_rs {$page->limit}";
$name_res = $dbh->query($name_sql);
$names = $name_res->fetchAll(PDO::FETCH_ASSOC);
?>
<?php
foreach ($names as $name) {
?>
<div class="box">
<div class="pic">
<p><?php echo $name['姓名']; ?></p>
</div>
</div>
<?php
}
?>
<div id="pagination">
<?php
if ($total > 0) {
echo $page->showPage();
} else {
echo "没有符合条件的结果。";
}
?>
</div>
搜索条件页面的代码就不写了啊,不然帖子太长了。