<?php
//分页:
//一页显示多少条数据:50
//总共会有多少页:ceil(总条数/50)
//当前是第几页:page = 1
//LIKE:SQL语句实现模糊查询
//%关键词%:包含 关键词 的所有记录, %是指匹配任意字符
//关键词%:以 关键词 开始的
//%关键词:以 关键词 结束的
//'SELECT * FROM shop360_top20_pc WHERE kw LIKE "%女装%"'; 精品女装/夏装
$pagenum = 50;
$page = $_GET ? $_GET['page'] : 1;
$key = @$_GET['kw'];
$c1 = @$_GET['c1'];
$c2 = @$_GET['c2'];
$c3 = @$_GET['c3'];
$wherestr = ' ';
$urlext = '';
if($key){
$wherestr .= ' AND kw LIKE "%'.$key.'%"';
$urlext .= '&key=' . $key;
}
if($c1){
$wherestr .= ' AND c1 LIKE "%'.$c1.'%"';
$urlext .='&c1=' . $c1;
}
if($c2){
$wherestr .= ' AND c2 LIKE "%'.$c2.'%"';
$urlext .='&c2=' . $c2;
}
if($c3){
$wherestr .= ' AND c3 LIKE "%'.$c3.'%"';
$urlext .='&c3=' . $c3;
}
//count()是SQL语句里面的一个函数,统计满足条件的记录数
//AS:取个别名
//查询结果排序:
//ORDER BY 字段 ASC==>是指按照该字段升序排列
//ORDER BY 字段 DESC==>是指按照该字段降序排列
//ORDER BY 字段1 ASC, 字段2 DESC, 字段3 DESC;
//$box = $mysql->selectData('shop360_top20_pc','count(*) AS nums',$whereStr);
//$shopnums = $box[0]['nums'];
//$pages = ceil($shopnums/$pagenum);
//$result = $mysql->selectData('shop360_top20_pc','*',$whereStr.' LIMIT '. ($page-1)*$pagenum .',' . $pagenum);
$sql = 'SELECT count(*) AS nums FROM shop360_top20_pc WHERE 1=1' . $wherestr . ' ORDER BY id ASC';
// echo $sql;
require './common.php';
$r = $db->query($sql);
$row = $r->fetch_array(MYSQLI_ASSOC);
// var_dump($row);
// 计算总页数
$totalpage = ceil($row['nums']/$pagenum);
//如何查询当前页面要显示的信息
//查询指定范围的数据:LIMIT start, recodenums
$sql = 'SELECT * FROM shop360_top20_pc WHERE 1=1'.$wherestr.' LIMIT '.($page - 1)*$pagenum.', ' . $pagenum;
$r = $db->query($sql);
$keyword = array();
while($kw = $r->fetch_array(MYSQLI_ASSOC)){
$keyword[] = $kw;
}
// var_dump($keyword);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>分页显示</title>
</head>
<body>
<!-- 模糊查询:LIKE -->
<form action="./page.php" method="get">
<input type="hidden" name="page" value="1">
关键词:<input type="text" name="kw" value="<?=$key;?>">
一级类目:<input type="text" name="c1" value="<?=$c1;?>">
二级类目:<input type="text" name="c2" value="<?=$c2;?>">
三级类目:<input type="text" name="c3" value="<?=$c3;?>">
<input type="submit" value="查询">
</form>
<!--循环展示信息-->
<table style="width: 100%; border-collapse: collapse;" border="1">
<tr>
<td>ID</td>
<td>关键词</td>
<td>一级类目</td>
<td>二级类目</td>
<td>三级类目</td>
</tr>
<?php
foreach ($keyword as $k => $value) {
# code...
echo '<tr>
<td>'.$value['id'].'</td>
<td>'.str_replace($key, '<span style="color:red;">'.$key.'</span>', $value['kw']).'</td>
<td>'.str_replace($c1, '<span style="color:yellow;">'.$c1.'</span>', $value['c1']).'</td>
<td>'.str_replace($c2, '<span style="color:greenyellow;">'.$c2.'</span>', $value['c2']).'</td>
<td>'.str_replace($c3, '<span style="color:blue;">'.$c3.'</span>', $value['c2']).'</td>
</tr>';
}
?>
</table>
<ul>
<li><a href="./page.php?page=<?=$urlext;?>">首页</a></li>
<?php
if($page > 1){
?>
<li><a href="./page.php?page=<?=($page-1).$urlext;?>">上一页</a></li>
<?php } ?>
<?php
$start = $page + 1;
if($totalpage - $page < 5){
$start = $totalpage - 4;
}
for($i = $start; $i < $page + 6 && $i <=$totalpage.$urlext; $i++){
echo '<li> <a href="./page.php??page='.$i.$urlext.'>">'.$i.'</a></li>';
}
?>
<!-- //显示当前页的后面五页链接 -->
<?php
if($page <$totalpage){
?>
<li><a href="./page.php?page=<?=($page+1).$urlext;?>">下一页</a></li>
<?php } ?>
<li><a href="./page.php?page=<?=$totalpage.$urlext;?>">尾页</a></li>
</ul>
</body>
</html>
liuyang.rar_文件的分页
版权申诉
122 浏览量
2022-09-20
23:14:31
上传
评论
收藏 2KB RAR 举报
局外狗
- 粉丝: 69
- 资源: 1万+
评论0