开发者

help with PHP pagination

开发者 https://www.devze.com 2022-12-18 04:03 出处:网络
hello guys im using this second part of pagination script to show pagination; <? if ($pageno == 1) {

hello guys im using this second part of pagination script to show 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
?>

like you see here: echo " <li class='active'>$pageno</li>"; i only can show first, prev, active page, next, last page..

my question is: i want to show more pages near active page.. how can i do this?

i me开发者_运维问答an pagination style is now like:

FIRST PREV 1 NEXT LAST

i want

FIRST PREV 1 2 3 4 5 6 7 NEXT LAST

thanks


Just add a loop which loops from the first to the last page numbers:

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>";

To let this work you'd need to find the maximum amount of items and divide it with the amount of items on a page:

$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

Of course you need to add some checks and stuff, this only shows the basic principles.


Between the part which displays the previous page links and the part which displays the next page links, you need a loop between 1 and total pages.

<?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>


<?
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
?>

this is fixed version. works fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号