分页,搜索后保留搜索条件分页

时间:2022-10-27 12:04:51

先写一个简单的分页,供初学者参考:


首先呢,在项目根目录创建文件a.php , 复制以下内容<注意提前下载jquery库>

<pre name="code" class="php">header("content-type:text/html;charset=utf8");
// 创建一个数据源,若数据来自于mysql ,此处请改为mysql获取数据 注意limit使用 limit($offset, $length) $offset 为偏移量,从第几个开始(下标从0开始) $length 是获取数据长度 此处获取结果为$arr.
$a = array(
array("id" => 1, "name" => "qwe"),
array("id" => 2, "name" => "fds"),
array("id" => 3, "name" => "gre"),
array("id" => 4, "name" => "cvb"),
array("id" => 5, "name" => "grh"),
array("id" => 6, "name" => "zxc"),
array("id" => 7, "name" => "Zxz"),
array("id" => 8, "name" => "fgf"),
);
// array_slice 数组截取
$arr = array_slice($a, 0, 2);


?>
<input type="hidden" id="page" value="1"/>
<input type="hidden" id="page_total" value="<?php echo ceil(count($a)/2); ?>"/>
<table id="content" border="1">
<tr><td>ID</td><td>name</td></tr>
<?php foreach ($arr as $v) { ?>
<tr>
<td><?php echo $v['id']; ?></td>
<td><?php echo $v['name']; ?></td>
</tr>
<?php } ?>
</table>
<span onclick="fun(1)" >首页</span>
<span onclick="fun(2)" >上一页</span>
<span onclick="fun(3)" >下一页</span>
<span onclick="fun(4)" >尾页</span>
<!--注意导入jquery库--><script src="jq.js" ></script><script type="text/javascript" >    // $flag 是为了区分当前操作    function fun (flag) {        var pageobj = $("#page");        var totalobj = $("#page_total");        var contentobj = $("#content");        var page = pageobj.val();        var total = totalobj.val();        // 当前操作后应该展示那一页的数据        var now = 1;        if (flag == 2) {            now = (page-1)>0 ? (page-1) : 1;        } else if (flag == 3) {            now = (page*1+1)<total ? (page*1+1) : total;        } else if (flag == 4) {            now = total        }        // 根据当前操作后应该展示的数据        $.ajax({            url:"b.php",            type:"get",            data:"now=" + now,            dataType:"json",            success:function (o) {                var str = "<tr><td>ID</td><td>name</td></tr>";                for(a in o) {                    str += "<tr><td>" + o[a].id + "</td><td>" + o[a].name + "</td></tr>"                }                contentobj.html(str);                pageobj.val(now);            }        });    }</script>
 

再创建文件b.php, 复制以下内容

// 数据源
$a = array(
array("id" => 1, "name" => "qwe"),
array("id" => 2, "name" => "fds"),
array("id" => 3, "name" => "gre"),
array("id" => 4, "name" => "cvb"),
array("id" => 5, "name" => "grh"),
array("id" => 6, "name" => "zxc"),
array("id" => 7, "name" => "Zxz"),
array("id" => 8, "name" => "fgf"),
);

// 获取当前页面应该展示的数据
$page = $_GET['now'] ? $_GET['now'] : 1;
$offset = ($page-1)*2;
$resurt = array_slice($a, $offset, 2);

// 返回数据 json
echo json_encode($resurt);

之后访问 http://yourDomainName/a.php,就可以看到所需要的效果了。


之后呢,我们需要搜索保留条件分页,其实只需要在原分页基础上对展示数据进行搜索处理,并且能够保证搜索条件保留,呢么我们的搜索分页也就完成了:

a.php

header("content-type:text/html;charset=utf8");
// 获取当前展示数据
$a = array(
array("id" => 1, "name" => "qwe"),
array("id" => 2, "name" => "fds"),
array("id" => 3, "name" => "gre"),
array("id" => 4, "name" => "cvb"),
array("id" => 5, "name" => "grh"),
array("id" => 6, "name" => "zxc"),
array("id" => 7, "name" => "Zxz"),
array("id" => 8, "name" => "fgf"),
array("id" => 9, "name" => "fgf"),
array("id" => 10, "name" => "fgf"),
array("id" => 11, "name" => "fgf"),
);
$arr = array_slice($a, 0, 2);
?>
<input type="hidden" id="page" value="1"/>
<input type="hidden" id="page_total" value="<?php echo ceil(count($a)/2); ?>"/>
<input type="text" id="sear" />
<input type="button" value="search" onclick="fun(1)"/>
<table id="content" border="1">
<tr><td>ID</td><td>name</td></tr>
<?php foreach ($arr as $v) { ?>
<tr>
<td><?php echo $v['id']; ?></td>
<td><?php echo $v['name']; ?></td>
</tr>
<?php } ?>
</table>
<span onclick="fun(1)" >首页</span>
<span onclick="fun(2)" >上一页</span>
<span onclick="fun(3)" >下一页</span>
<span onclick="fun(4)" >尾页</span>
<script src="jq.js" ></script>
<script type="text/javascript" >
function fun (flag) {
// 分页数据
var pageobj = $("#page");
var totalobj = $("#page_total");
var contentobj = $("#content");
var page = pageobj.val();
var total = totalobj.val();
var sear = $("#sear").val();

// 获取操作后应当展示页数
var now = 1;

if (flag == 2) {
now = (page-1)>0 ? (page-1) : 1;
} else if (flag == 3) {
now = (page*1+1)<total ? (page*1+1) : total;
} else if (flag == 4) {
now = total
}
$.ajax({
url:"b.php",
type:"get",
data:"now=" + now + "&sear=" + sear,
dataType:"json",
success:function (o) {
// 返回结果拼接字符串,替换
var str = "<tr><td>ID</td><td>name</td></tr>";
for(a in o['resurt']) {
str += "<tr><td>" + o['resurt'][a].id + "</td><td>" + o['resurt'][a].name + "</td></tr>"
}

contentobj.html(str);
pageobj.val(now);
totalobj.val(o['count']);
}
});
}
</script>

b.php

$a = array(
array("id" => 1, "name" => "qwe"),
array("id" => 2, "name" => "fds"),
array("id" => 3, "name" => "gre"),
array("id" => 4, "name" => "cvb"),
array("id" => 5, "name" => "grh"),
array("id" => 6, "name" => "zxc"),
array("id" => 7, "name" => "Zxz"),
array("id" => 8, "name" => "fgf"),
array("id" => 9, "name" => "fgf"),
array("id" => 10, "name" => "fgf"),
array("id" => 11, "name" => "fgf"),
);
// 获取数据
$page = $_GET['now'] ? $_GET['now'] : 1;
$sear = $_GET['sear'] ? $_GET['sear'] : "";

$b = $a;
// 搜索 此处应该改为数据库搜索,只是做一个模拟
if (!empty($sear)) {
$b = array();
foreach ($a as $k => $v) {
if ($v['name'] == $sear) {
// echo $v['id'];
$b[] = $v;
}
}
}

$offset = ($page-1)*2;
$resurt = array_slice($b, $offset, 2);
$count = ceil(count($b)/2);

// 处理结果返回
echo json_encode(array('resurt' => $resurt, 'count' => $count));

浏览器刷新a.php,看到的就是你想要的结果了!!! 分页,搜索后保留搜索条件分页