1

Possible Duplicate:
Break up PHP Pagination links

I have used a simple paging on my web page and it is working. However, my problem is that when the paging links reach numerous amount like

Page: [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15] and so on.

I wanted to cut it to five links and then show a button for the next 5 links like:

Page: [1],[2],[3],[4],[5] [Next 5].

When I click the [Next 5], It will show [Prev 5] [6],[7],[8],[9],[10] [Next 5] and the current page is at page 6.

(Bold character denotes current page.)

Here is the code to be modified:

$perpage = 10;

if(isset($_GET["pagenum"])) {
  $page = intval($_GET["pagenum"]);
} else {
  $page = 1;
}
$calc = $perpage * $page;
$start = $calc - $perpage;

$orders_count = 150;
$rowss = $orders_count;

if($rowss) {
   $total = $orders_count;
}

$totalPages = ceil($total / $perpage);

if($page <=1 ) {
  $feedbacks .= "";
} else {
  $j = $page - 1;
  $feedbacks .= "<a class='first-page' title='Go to the first page' href='" . $path . "&pagenum=$j'>&laquo;</a>";
}

for($i=1; $i <= $totalPages; $i++) {
  if($i<>$page) {
     $feedbacks .= "<a class='first-page' title='Go to the first page' href='" . $path . "&pagenum=$i'>$i</a>";
  } else {
     $feedbacks .= "<a class='first-page disabled' title='Go to the first page' href='#'>$i</a>";
  }
}

if($page == $totalPages ) {
  $feedbacks .= "";
} else {
  $j = $page + 1;
  $feedbacks .= "<a class='last-page' title='Go to the first page' href='" . $path . "&pagenum=$j'>&raquo;</a></span>";
}

$feedbacks .= "</div>";
echo $feedbacks;
Community
  • 1
  • 1
Ken
  • 833
  • 2
  • 13
  • 27

1 Answers1

1

that was an interesting question :)

hope it helps.

<?php
//set params
$total = 283;
$maxPerPage = 10;
$cutStarting = 5;
$totalPages = ceil($total / $maxPerPage);
$prevPageNum = 0;

//set currentPageId
$currentPage = isset($_GET['pagenum']) ? intval($_GET['pagenum']) : 1;

if ($currentPage > $totalPages && $currentPage < 1) {
    $currentPage = 1;
}

//set first row (maybee for limits in mysql query)
$firstRow = ($currentPage * $maxPerPage) - ($maxPerPage) + 1;

//calculate prev link pageId
if ($currentPage > $cutStarting) {
    $prevPageNum = floor(($currentPage - 1) / $cutStarting) * $cutStarting;
    echo '[ <a href="?pagenum='.$prevPageNum.'">Prev 5</a> ] ';
}

//generate number page links
$links = '';
$first = ($prevPageNum + 1);
$last = $first + $cutStarting;

for ($i=($prevPageNum + 1); $i < $last; $i++) {
    if ($i <= $totalPages) {
        if ($currentPage == $i) {
            $links .= '[ <b>'.$i.'</b> ] ';
        } else {
            $links .= '[ <a href="?pagenum='.$i.'">'.$i.'</a> ] ';
        }
    }
}

echo ' '.$links;

//calculate next link pageId
$nextPageNum = ( ceil( $currentPage / $cutStarting ) * $cutStarting ) + 1;
if ($nextPageNum <= $totalPages) {
    echo '[ <a href="?pagenum='.$nextPageNum.'">Next 5</a> ] ';
}


//get some infos for testing
echo '<br /><br />Testing<hr>';
echo 'Total Pages: '.$totalPages.'<br />';
echo 'Current Page: '.$currentPage.'<br />';
echo 'First Row: '.$firstRow;
?>

note: do things like this in javascipt, is better for the server ;)

spinsch
  • 1,415
  • 11
  • 23
  • +1 for javascript suggestion – Sir Dec 31 '12 at 08:45
  • @rcro What about `$calc` and `$start` on my code? I use those variables on the mysql query like `LIMIT " . $start . ", " . $perpage` Thank you rcro – Ken Dec 31 '12 at 08:50
  • @Ken your $start is equals with $firstRow in my exectutable example. – spinsch Dec 31 '12 at 09:02
  • @rcro I tried that sir the problem is that there is only 1 result displayed even though the maxpage is 10 and the total result is for example is 20 and i also notice that the last row is being displayed instead of the 1st row first(I already checked my query and its fine with the order). Here is the pastebin of my implementation of your code. http://pastebin.com/VtgYwijg. Please take a look at it if I mistakenly implemented your code. – Ken Dec 31 '12 at 09:17
  • @Ken i've seen the limit begins with 0 in mysql-query. look at line 9, i've updated the $firstRow: http://pastebin.com/MXVFPdjS . hope that helps. – spinsch Dec 31 '12 at 10:52
  • @rcro the problem now after re-applying your code is that when going to next pages, only the first row is being displayed. For example, the total result generated from the query is 2 and i set the max per page to 1 so it will become 2 pages. The first page will display the first row and the second last page will display the 2nd row however, the problem is it does not.. The second last page displays the first row instead of the 2nd row. So what i did is to modify line 13 to `$firstRow = ( $currentPage -1 ) * $maxPerPage`. Please tell if if its fine. – Ken Dec 31 '12 at 23:22