一、分页样式与结果
分页的页码总共显示10个,如果需要分的页超过10个就将显示2~11,3~12…,15~24等依次类推,界面依旧显示10个可操作页码。

 具体的CSS样式,参考Bootstrap
https://v3.bootcss.com/components/
二、PHP函数分析
1、数据库查询操作函数分析
为了方便编码时更加方便的进行逻辑性分析,将一些重复性的操作,写成函数,封装到另一个PHP文件中,此处的数据库查询操作将实现,在给定页码大小后,返回当前页码下应该显示的数据,数据的格式是数组格式。
function DataPageShow($PageNum){
$skip=($PageNum-1)*10;
$link=mysqli_connect('localhost','root','','mysqltest');
mysqli_set_charset($link,'utf8');
$sql="select * from sinanews limit {$skip},10";
$ExeResult=mysqli_query($link,$sql);
$DataResult=mysqli_fetch_all($ExeResult,MYSQLI_ASSOC);
mysqli_close($link);
return $DataResult;
}
2、数据表格显示函数分析
从数据库查询的数据都是数组格式的,然后以表格方式来展现出来,所以此部分函数功能就是,给定具体要显示的数组格式数据,然后转换成HTML标签显示出来
function DataShow($ShowData){
foreach($ShowData as $index => $val){
if($index%2){
$color='lightblue';
}else{
$color='yellow';
}
$IndexId=($index%10)+1;
$tim=date('Y-n-d H:i:s',$val['inputtime']);
echo "
<tr style='background:{$color}'>
<th class='ids'>{$IndexId}</th>
<th class='cur'>{$val['title']}</th>
<th class='cur'>{$val['keywords']}</th>
<th class='cur'>{$val['author']}</th>
<th style='width:310px;height:80px;text-align:center;'>{$val['content']}</th>
<th class='imag'><img src='{$val['image']}' style='zoom: 15%;'></img></th>
<th class='cur'>{$tim}</th>
<th class='cur'><a>修改</a> <a>删除</a></th>
</tr>";
}
}
三、HTML部分的逻辑划分分析
1、分页类型判断
分页的页码根据限制条件,总共显示10个页码,来分析,有两个大方向,一、数据量不足以分10个页码时;二、在数据量的分页超过10个页码时。
2、分页页码不足
因为页码不足,所以页面显示的页码数,应该根据数据的具体条数来确定,具体代码如下:
<?php
$AllRows=DataRowNumber();
$AllPages=ceil($AllRows/10);
if($AllPages<=10):
for($i=1;$i<=$AllPages;$i++):
?>
<li><a href="./ShowNews.php?pages=<?= $i; ?>"><?= $i;?></a></li>
<?php
endfor;
endif;
?>
3、页码溢出
在页面的页码数最大为10,当分页超过10个页码时,用滚动的方式来实现页码的显示如:13~22,具体的代码判断如下:
<table>
<form action="#" method="GET" name='paging' >
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="./ShowNews.php?pages=<?= ($NowPages-1); ?>" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<?php
$AllRows=DataRowNumber();#获取数据的所有行数
$AllPages=ceil($AllRows/10);#计算需要设置的分页数
if($AllPages<=10):
for($i=1;$i<=$AllPages;$i++):
?>
<li><a href="./ShowNews.php?pages=<?= $i; ?>"><?= $i;?></a></li>
<?php
endfor;
endif;
//页码超过10个时的判断
if($AllPages>10):
for($j=1;$j<=10;$j++):
if($NowPages>10): //当前页码值超过10,代表目前页码数满足不了目前需求,所以,使用滚动页码
$key=$j+($NowPages-10);//实现页码变动
?>
<li><a href="./ShowNews.php?pages=<?= $key; ?>"> <?= $key; ?> </a></li>
<?php
endif;
if($NowPages<=10)://当前页码值没超过10,代表目前页码数满足目前需求,所以,使用固定页码
?>
<li><a href="./ShowNews.php?pages=<?= $j; ?>"> <?= $j; ?> </a></li>
<?php
endif;
endfor;
endif;
?>
<li>
<a href="./ShowNews.php?pages=<?= ($NowPages+1); ?>" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
<form>
</table>
|