帮助PHP分页

时间:2010-01-28 10:00:19

标签: php mysql html pagination

你好我们使用这个分页脚本的第二部分来显示分页;

<?
if ($pageno == 1) {
   echo "<li class='previous-off'>«« İlk Sayfa</li> <li class='previous-off'>« Önceki Sayfa</li> ";
} else {
   echo " <li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=1'>«« İlk Sayfa</a></li> ";
   $prevpage = $pageno-1;
   echo "<li> <a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$prevpage'>« Önceki Sayfa</a> </li>";
} // if
echo " <li class='active'>$pageno</li>";
if ($pageno == $lastpage) {
   echo " <li class='previous-off'>«« Sonraki</li> <li class='previous-off'>« Son Sayfa</li> ";
} else {
   $nextpage = $pageno+1;
   echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$nextpage'>Sonraki »</a></li> ";
   echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$lastpage'>Son Sayfa »»</a></li> ";
} // if
?>

就像你在这里看到的那样:echo " <li class='active'>$pageno</li>";我只能显示第一页,上一页,活动页面,下一页,最后一页..

我的问题是:我想在活动页面附近显示更多页面..我该怎么做?

我的意思是分页风格现在就像:

  

第一个上一个下一个

我想要

  

首页上一页1 2 3 4 5 6 7下一页

感谢

3 个答案:

答案 0 :(得分:2)

只需添加一个从第一页到最后一页循环的循环:

for($page_number = 1; $page_number <= $amount_of_pages; $page_number++)
    if($page_number == $pageno)
        echo " <li class='active'>$page_number (active)</li>";
    else
        echo " <li class='active'><a href="...">$page_number</a></li>";

为了让这项工作,您需要找到最大数量的项目,并将其除以页面上的项目数量:

$result = mysql_query("SELECT COUNT(*) FROM table");
$row = mysql_fetch_row($result);
$amount_of_items = $row[0];
$amount_of_pages = $amount_of_items / 10; // 10 items on a page

当然,您需要添加一些检查和内容,这只显示基本原则。

答案 1 :(得分:0)

在显示上一页链接的部分和显示下一页链接的部分之间,需要一个介于1和总页数之间的循环。

<?php
$currentPage = 3;
$totalPages = 10;
?>

<a href="">First page</a>
<a href="">Prev page</a>

<?php
for ($i = 0; < $totalPages; $i++) {
    printf('<a href="%s?page=%d" class="%s">Page %d</a>', $_SERVER['PHP_SELF'], $i, ($i == $currentPage ? 'active' : ''), $i);
}
?>

<a href="">Next page</a>
<a href="">Last page</a>

答案 2 :(得分:0)

<?
if ($pageno == 1) {
   echo "<li class='previous-off'>«« İlk Sayfa</li> <li class='previous-off'>« Önceki Sayfa</li> ";
} else {
   echo " <li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=1'>«« İlk Sayfa</a></li> ";
   $prevpage = $pageno-1;
   echo "<li> <a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$prevpage'>« Önceki Sayfa</a> </li>";
} // if
for($page_number = 1; $page_number <= $lastpage; $page_number++)
if($page_number == $pageno) {
echo "<li class='active'>$pageno</li>";
}
else {
echo "<li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$page_number'>$page_number</a></li>";
}

if ($pageno == $lastpage) {
   echo " <li class='previous-off'>«« Sonraki</li> <li class='previous-off'>« Son Sayfa</li> ";
} else {
   $nextpage = $pageno+1;
   echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$nextpage'>Sonraki »</a></li> ";
   echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$lastpage'>Son Sayfa »»</a></li> ";
} // if
?>

这是固定版本。工作正常。