本文介绍了如何在分页中添加省略号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助在分页脚本中添加省略号.我在Google上找不到任何简单的东西.所以这就是为什么我在这里问.谢谢!!!这是代码;
I need help adding an ellipsis to my pagination script. I can't find anything simple on Google. So that's why I ask on here. Thank you!!! Here is the code;
if (isset($_GET['p'])) {
$pageno = $_GET['p'];
} else {
$pageno = 1;
} // if
$query = mysql_query("SELECT count(id) FROM m3_musicblog");
$query_data = mysql_fetch_row($query);
$numrows = $query_data[0];
$rows_per_page = 4;
$lastpage = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
} // if
if ($pageno < 1) {
$pageno = 1;
} // if
$limit = 'ORDER BY id DESC LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = mysql_query("SELECT * FROM m3_musicblog $limit");
if ($pageno == 1) {
echo "<a>««</a> <a>«</a> ";
} else {
echo " <a href='music-news?p=1'>««</a> ";
$prevpage = $pageno-1;
echo " <a href='music-news?p=$prevpage'>«</a> ";
} // if
for($page_number = 1; $page_number <= $lastpage; $page_number++)
if($page_number == $pageno) {
echo "<span class='current'>$pageno</span>";
}
else {
echo "<a href='music-news?p=$page_number' class='page' title='$page_number'>$page_number</a>";
}
if ($pageno == $lastpage) {
echo " <a>»</a> <a>»»</a> ";
} else {
$nextpage = $pageno+1;
echo " <a href='music-news?p=$nextpage'>»</a> ";
echo " <a href='music-news?p=$lastpage'>»»</a> ";
} // if
推荐答案
这应该做到:
$printed_ellipsis = false;
for($page_number = 1; $page_number <= $lastpage; $page_number++) {
if ($page_number > 4 && !$printed_ellipsis) {
echo "…" // horizontal ellipsis character
$page_number = max(0, $lastpage - 3);
$printed_ellipsis = true;
}
if($page_number == $pageno) {
echo "<span class='current'>$pageno</span>";
}else{
echo "<a href='music-news?p=$page_number' class='page' title='$page_number'>$page_number</a>";
}
}
很抱歉,如果我的PHP有点过时了(已经有一段时间了),但是您应该能够了解这段代码的作用.
Sorry if my PHP is a bit off (it's been a while) but you should be able to get the idea of what this code does.
这篇关于如何在分页中添加省略号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!