PHP通用的分页组件(分页加绑定数据)
最后更新于:2022-04-01 21:42:15
**最近整理了一下项目中用到的一些分页组件,将其封装起来(可以说1.0版本),希望大侠们多提提意见,来,先看一下运行效果**
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/0510d21c7f7818f684f668ef4c283376_727x340.gif)
样式可能不是太美观,不过我已将样式与功能分离,这样大家就可以根据自己的需要的风格定义样式了,先说下我这个组件的**优点:**
**
**1.将分页与数据绑定合在一个文件输出**
2**.灵活性高:不局限于某一个表,我们可以通过传表名来获取不同的结果,表内字段名完全自动识别**
**3.可以按照条件查询数据**
**4.每页显示的条数也只是一个参数**
**下面看一下代码**
~~~
1)//如果页数大于1进行分页
{
$pagination .= "";
echo '
";
echo $pagination;
}
?>
~~~
**下面看一下调用方法 调用很简单只是根据情况传递参数**
**
~~~
$page='ManageNews.php';//要提交的页面
$adj=3;//要查询的页面两边各放几个按钮
$limit=10;//每页要显示的条数
$table='news';//查询的表名
$strwhere=" where sort="."'".$sort."'"; //查询的where 条件
$addPage='AddNews.php?newid=';//添加要跳转的页面
$sql="SELECT id, title, addDate, (CASE sort WHEN '0' THEN '科技新闻' WHEN '1' THEN '最新活动' WHEN '2' THEN '技术服务' WHEN '3' THEN '诚聘英才' END ) AS sort, click FROM `news`" ; //要执行的查询语句
$arrayTitle=array('编号','标题','类型','发布时间','点击次数','修改','删除');//数据绑定每列的标题 必须和下面的每列的列名对应
$arrayColName=array('id','title','sort','addDate','click','xg1125','sc1125');//数据表中要显示的列名,当为xg1125时表示修改,sc1125删除
echoPage($page,$adj,$limit,$table,$sql,$arrayTitle,$arrayColName,$strwhere,$sort,$addPage);
~~~
**以上就是我整理的分页逻辑,自己发现的问题:健壮性有待提高,我会尽快的完善一下,希望大家能够多提提意见,小弟不胜感激,我定会虚心接受。**
';
";
//previous button 上一页
if ($page > 1)
$pagination.= "« previous";
else
$pagination.= "« previous";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //页面小于13页,全部显示
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "$counter";
else
$pagination.= "$counter";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //页面大于11页隐藏部分分页
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2)) //如果分页值为7一下的显示方式
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)//显示1搭配9
{
if ($counter == $page)
$pagination.= "$counter";
else
$pagination.= "$counter";
}
$pagination.= "...";
$pagination.= "$lpm1";
$pagination.= "$lastpage";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))//如果分页值离左右边界的距离都大于6
{
$pagination.= "1";
$pagination.= "2";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)//则显示分页值为中心左边6个右边6个
{
if ($counter == $page)
$pagination.= "$counter";
else
$pagination.= "$counter";
}
$pagination.= "...";
$pagination.= "$lpm1";
$pagination.= "$lastpage";
}
//close to end; only hide early pages
else
{
$pagination.= "1";
$pagination.= "2";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "$counter";
else
$pagination.= "$counter";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "next »";
else
$pagination.= "next »";
$pagination.= "
\n";
}
//实现绑定数据功能
echo "ID | "; //echo'标题 | '; //echo'类型 | '; //echo'发布时间 | '; //echo'点击次数 | '; //echo'修改 | '; //echo'删除 | '; foreach($arrayTitle as $title) { echo''.$title.' | '; } echo '|||
---|---|---|---|---|---|---|---|---|---|---|
".$row['title']." | "; else if($col=='xg1125') echo "修改 | "; else if($col=='sc1125') echo "删除 | "; else echo "".$row[$col]." | "; //正常情况下的输出 } //echo "".$row['ID']." | "; //echo "".$row['title']." | "; //echo ".".$row['sort']." | "; //echo ".".$row['Date']." | "; //echo "".$row['click']." | "; //echo "修改 | "; //echo "删除 | "; echo '