0

My pagination is showing a page zero (0) when you go to page 2. Not sure why. I don't want to show a page zero.

I'll try to only show the necessary code.

Here is my code:

 <?php
 $rec_limit = 100;      
 $targetpage = "dispatch.php";  
 if (isset($_GET['page'])) 
 {
   $page = $_GET['page'];
   $offset = $rec_limit * ($page - 1);
 }
 else
 {
   $page = 1;
   $offset = 0;
 }
 *** $left_rec = countRecords() - ($page * $rec_limit); ***

 $total_records = countRecords(); // countRecords() should be self-explanatory
 $total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100
 $adjacents = 2;
 $previousPage = $page - 1;
 $nextPage = $page + 1;
 $querystring = "";
 $start = ($page < $adjacents ? 1 : $page - $adjacents); // <-- i think the issue is here
 $beginning = 1;
 $end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);

 foreach ($_GET as $key => $value)
 {
   if($key != "page") $querystring .= "$key=$value&amp;";
 }
 echo "<div class="row-fluid"><div class="span2"><ul class="pager"><li><a href=\"$targetpage?page=$beginning&$querystring\">First</a></li>";
 if ($left_rec < $rec_limit) 
 {
   $last = $page - 1;
   echo @"<li><a href=\"$targetpage?page=$previousPage&$querystring\">Previous</a></li>";
   for($i= $start; $i <= $end; $i++)
   {
     echo "<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
   }
 }
 else if($page == 0)
 {
   for($i= $start; $i <= $end; $i++)
   {
     echo "<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
   }
   echo "<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a></li>";
 }
 else if ($page > 0)
 {
   $last = $page - 2;
   echo "<li><a href=\"$targetpage?page=$previousPage&$querystring\">Previous</a></li> ";
   for($i= $start; $i <= $end; $i++)
   {
     echo @"<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
   }
   echo "<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a></li>";
 }
 echo "<li><a href=\"$targetpage?page=$total_pages&$querystring\">Last</a></li>";   
 echo '</ul></div></div>';
 ?>

I would really appreciate the help in removing page 0 from the application. Please disregard any typos or missing quotes. The code works with the exception of it showing page 0.

I added a picture of what the application showing page 0. It only shows page 0 when I go to page 2. After that, I no longer see page 0.

enter image description here

Please let me know what I have to do.

Thanks.

HoodCoderMan
  • 103
  • 7
  • 26

2 Answers2

3

Some advice:

  1. You really shouldn't suppress errors using @, instead you should be instantiating all of your variables and writing proper code.

  2. Don't hard-code pagination into each page. Instead, wrap it in a reusable function.

Example:

// draws a menu for navigating multiple pages of content
function paginate($page, $display, $total) {
    if(isset($_SERVER['QUERY_STRING']) && trim($_SERVER['QUERY_STRING']) != '') {
        if(stristr($_SERVER['QUERY_STRING'], 'page=')) {
            $query = '?' . preg_replace('/page=\d+/', 'page=', $_SERVER['QUERY_STRING']);
        } else {
            $query = '?' . $_SERVER['QUERY_STRING'] . '&page=';
        }
    } else {
        $query = '?page=';
    }

    $pages = $total <= $display ? 1 : ceil($total / $display);
    $self = htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8');
    $first = '<a href="' . $self . $query. '1">first</a>';
    $prev = '<a href="' . $self . $query . ($page - 1) . '">prev</a>';
    $next = '<a href="' . $self . $query . ($page + 1) . '">next</a>';
    $last = '<a href="' . $self . $query . $pages . '">last</a>';

    echo '<p>';
    echo ($page > 1) ? "$first | $prev |" : 'first | prev |';
    echo '(page ' . $page . ' of ' . $pages . ')';
    echo ($page < $pages) ? "| $next | $last" : '| next | last';
    echo '</p>';
}

// output example
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$display = 100;
$start = $display * $page - $display;
$total = countRecords($start, $display);

paginate($page, $display, $total);
mister martin
  • 6,197
  • 4
  • 30
  • 63
  • 1
    This is vulnerable to XSS, you should sanitize `$_SERVER['PHP_SELF']`, as explained [here](http://stackoverflow.com/questions/6080022/php-self-and-xss). – Matthew Johnson May 07 '14 at 14:09
0

I agree with @mister martin, but if you use this code fundamentally, try to change

$start = ($page < $adjacents ? 1 : $page - $adjacents);

To:

$start = ($page < $adjacents ? $page : $page - $adjacents);

Edited: maybe problem in undefined $left_rec, check demo. You can change $s like $_GET['page'] and as I see all works correctly.

Demo.

<?php
$rec_limit = 100;
$targetpage = "dispatch.php";
$total_records = countRecords();

// Nav part
$page = intval($_GET['page'])? $_GET['page']: 1;
$offset = $rec_limit * ($page - 1);
$total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100

$adjacents = 2;
$previousPage = $page - 1;
$nextPage = $page + 1;
$start = ($page < $adjacents ? $page : $page - $adjacents); // <-- I think the issue is this line
$beginning = 1;
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);

$uri = $_GET;
unset($uri['page']);
$querystring = http_build_query($uri);

echo '<div class="row-fluid"><div class="span2"><ul class="pager"><li><a href="'.$targetpage.'?page='.$beginning.'&'.$querystring.'">First</a></li>';
 if($left_rec < $rec_limit && ($page > 1))
 {
     echo "<li><a href=\"$targetpage?page=$previousPage&$querystring\">Previous</a></li>";
     for($i= $start; $i <= $end; $i++)
     {
         echo "<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
     }
 } else if ($page > 1)
 {
     echo "<li><a href=\"$targetpage?page=$previousPage&$querystring\">Previous</a></li> ";
     for($i= $start; $i <= $end; $i++)
     {
         echo "<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
     }
     echo "<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a></li>";
 } else {
     for($i= $start; $i <= $end; $i++)
     {
         echo "<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
     }
     echo "<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a></li>";
 }
 echo "<li><a href=\"$targetpage?page=$total_pages&$querystring\">Last</a></li>";  
 echo '</ul></div></div>';
 ?>
Alexey Palamar
  • 1,440
  • 1
  • 10
  • 16
  • your answer looked really promising, and in fact, was the best response I've gotten in 2 days. However, the output remained the same. Any thoughts? – HoodCoderMan May 08 '14 at 14:54
  • same results. I added $left_rec in my code above. I still get a page 0. I hope you will get back to me with a new update to your code. – HoodCoderMan May 08 '14 at 18:43
  • I commented $left_rec because I don't where this variable defined, can you provide code for getting value of $left_rec? – Alexey Palamar May 08 '14 at 19:59
  • Well, I added the code above for $left_rec. Is that not what you're looking for? Please let me know. – HoodCoderMan May 09 '14 at 13:47