HoodCoderMan
HoodCoderMan

Reputation: 103

PHP remove page 0 from pagination

I was able to get my pagination script working, and everything works fine except when you go to PAGE 2, you can see a PAGE 0, which if you click on it give you nothing.

I need to fix my current code so that the application does not show a PAGE 0.

Here is the necessary code:

 <?php
   $total_records = countRecords(); // self explanatory function called here
   $total_pages = ceil($total_records / $rec_limit);
   $adjacents = 2;
   $previousPage = $page - 1;
   $nextPage = $page + 1;
   $querystring = "";
   $start = ($page < $adjacents ? 1 : $page - $adjacents);
   $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">'.countRecords()." total records" .'</div>
      <div class="container pagination-small">
      <ul style="margin: 3px;" class="pager">';
   echo @"<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 tried to keep the code as short as possible. If there are any errors, just note it's only a typo as I typed it here. The code works, with exception to my problem.

So with that all said, can anyone tell me how to remove PAGE 0 from the pagination? I have done some research, but I have been unsuccessful applying it to my code. So I'm hoping someone can take a look at my code and tell me how I can alter it to make this work.

I appreciate the help.

Thank you in advance.

Upvotes: 0

Views: 534

Answers (1)

Marc B
Marc B

Reputation: 360702

Logic flaw:

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

If you're on Page 1, you'll get

$start = (1 < 2 ? 1 : 1 - 2);
$start = -1; // page negative one? huh?

Then this loop is pointless:

foreach($_GET as $key => $value) { ... }

Why not just

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

Upvotes: 1

Related Questions