ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**最近整理了一下项目中用到的一些分页组件,将其封装起来(可以说1.0版本),希望大侠们多提提意见,来,先看一下运行效果** ![](https://box.kancloud.cn/0510d21c7f7818f684f668ef4c283376_727x340.gif) 样式可能不是太美观,不过我已将样式与功能分离,这样大家就可以根据自己的需要的风格定义样式了,先说下我这个组件的**优点:** ** **1.将分页与数据绑定合在一个文件输出** 2**.灵活性高:不局限于某一个表,我们可以通过传表名来获取不同的结果,表内字段名完全自动识别** **3.可以按照条件查询数据** **4.每页显示的条数也只是一个参数** **下面看一下代码** ~~~ <?php include("../Common/connectDB.php"); //分页函数 //$page要提交的页面 //$adj要查询的页面两边各放几个按钮 //$limit每页要显示的条数 //$table查询的表名 //$sql 要执行的查询语句 //$arrayTitle 数据绑定每列的标题 必须和下面的每列的列名对应 //$arrayColName 数据表中要显示的列名,当为xg1125时表示修改,sc1125删除 //$strwhere查询的where条件 //$sort用于保存查询条件的值 //$addPage 添加,修改时要跳转的页面 function echoPage($fname,$adj,$limi,$table,$sql,$arrayTitle,$arrayColName,$strwhere,$sort,$addPage) //传一个要显示的字段数组 { $tbl_name=$table; //要查询的表名 // 要查询的页面两边各放几个按钮 $adjacents = $adj; $query = "SELECT COUNT(*) as num FROM $tbl_name".$strwhere; $total_pages = mysql_fetch_array(mysql_query($query)); @$total_pages = $total_pages[num];//得到总条数 /* Setup vars for query. */ $targetpage = $fname; //将要进行分页的页面用来向该文件传递参数 $limit = $limi; //每页显示多少条数据 if(isset($_GET['page'])) $page = $_GET['page']; //要显示的那一页 else $page=1; if($page) $start = ($page - 1) * $limit; //first item to display on this page,本页的第一条数据位置 else $start = 0; //if no page var is given, set start to 0 如果没有传递参数值,则默认为0 /* Get data. */ $sql = $sql .$strwhere. "order by addDate desc LIMIT $start, $limit " ; //echo $sql; $result = mysql_query($sql); /* Setup page vars for display. */ if ($page == 0) $page = 1; //if no page var is given, default to 1. 如果没有提供页数则为当前页 $prev = $page - 1; //previous page is page - 1 前一页的页号 $next = $page + 1; //next page is page + 1 下一页的页号 $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. 最后一页 $lpm1 = $lastpage - 1; //last page minus 1 倒数第二页 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination = "";//保存前台显示按钮 //分页逻辑 if($lastpage > 1)//如果页数大于1进行分页 { $pagination .= "<div class=\"pagination\">"; //previous button 上一页 if ($page > 1) $pagination.= "<a href=\"$targetpage?sort=$sort&page=$prev\">« previous</a>"; else $pagination.= "<span class=\"disabled\">« previous</span>"; //pages if ($lastpage < 7 + ($adjacents * 2)) //页面小于13页,全部显示 { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?sort=$sort&page=$counter\">$counter</a>"; } } 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.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?sort=$sort&page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?sort=$sort&page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?sort=$sort&page=$lastpage\">$lastpage</a>"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))//如果分页值离左右边界的距离都大于6 { $pagination.= "<a href=\"$targetpage?sort=$sort&page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?sort=$sort&page=2\">2</a>"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)//则显示分页值为中心左边6个右边6个 { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?sort=$sort&page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?sort=$sort&page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?sort=$sort&page=$lastpage\">$lastpage</a>"; } //close to end; only hide early pages else { $pagination.= "<a href=\"$targetpage?sort=$sort&page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?sort=$sort&page=2\">2</a>"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?sort=$sort&page=$counter\">$counter</a>"; } } } //next button if ($page < $counter - 1) $pagination.= "<a href=\"$targetpage?sort=$sort&page=$next\">next »</a>"; else $pagination.= "<span class=\"disabled\">next »</span>"; $pagination.= "</div>\n"; } //实现绑定数据功能 echo "<table id='mytable' cellspacing='0'>"; echo '<tr>'; //echo"<th class='hide'>ID</th>"; //echo'<th>标题</th>'; //echo'<th>类型</th>'; //echo'<th>发布时间</th>'; //echo'<th>点击次数</th>'; //echo'<th>修改</th>'; //echo'<th>删除</th>'; foreach($arrayTitle as $title) { echo'<th>'.$title.'</th>'; } echo '</tr>'; //在下面的循环里可以稍微加点逻辑 while($row=@mysql_fetch_array($result)) { echo '<tr>'; foreach($arrayColName as $col) { if($col=='title') echo "<td><a href='#'>".$row['title']."</a></td>"; else if($col=='xg1125') echo "<td><a href='$addPage".$row['id']."'>修改</a></td>"; else if($col=='sc1125') echo "<td><a href='$targetpage?sort=$sort&delid=".$row['id']."' onclick=\"if (confirm('删除之后将无法恢复,确定要删除吗?')) return true; else return false;\" >删除</a></td>"; else echo "<td>".$row[$col]."</td>"; //正常情况下的输出 } //echo "<td>".$row['ID']."</td>"; //echo "<td><a href='#'>".$row['title']."</a></td>"; //echo "<td>.".$row['sort']."</td>"; //echo "<td>.".$row['Date']."</td>"; //echo "<td>".$row['click']."</td>"; //echo "<td><a href='#'>修改</a></td>"; //echo "<td><a href='#'>删除</a></td>"; echo '</tr>'; } echo "</table>"; 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); ~~~ **以上就是我整理的分页逻辑,自己发现的问题:健壮性有待提高,我会尽快的完善一下,希望大家能够多提提意见,小弟不胜感激,我定会虚心接受。**