GlenR
GlenR

Reputation: 95

Pagination not showing any results after 1st page

I'm using this pagination script, and it is showing the correct number of records for the first page, and the correct number of page links. When i click a page link however, the other pages show no records or page links at all.

Can anyone see where the problem is?

Thanks for looking.............

if(isset($_GET['brand'])){
   $brand = $_GET['brand'];
   $sql = mysqli_query($link, "SELECT COUNT(id) FROM products WHERE brand = '$brand' 
                       AND status = 1 ORDER BY id DESC") OR die(mysqli_error($link));
   $r = mysqli_fetch_row($sql);
   $numrows = $r[0];
   // number of rows to show per page
   $rowsperpage = 1;
   // find out total pages
   $totalpages = ceil($numrows / $rowsperpage);
   // get the current page or set a default
   if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];} 
   else {
   // default page num
   $currentpage = 1;} // end if
   if ($currentpage > $totalpages) {
   $currentpage = $totalpages;} // end if
   // if current page is less than first page...
   if ($currentpage < 1) {
      $currentpage = 1;} // end if
      $offset = ($currentpage - 1) * $rowsperpage;
      // get the info from the db 
      $sql = mysqli_query($link, "SELECT *FROM products WHERE  brand = '$brand' AND 
                          status 1 ORDER BY id DESC LIMIT $offset, $rowsperpage") OR 
                        die(mysqli_error($link));
      echo"<div class='brandheading'>",
      $brand,
      "</div>";
      if (!mysqli_num_rows($sql)){
         echo 'No Products Match That Brand';}
      else{
         /******  build the pagination links ******/
         echo" <div class='pagination'>";
         // range of num links to show
         $range = 3;
         // if not on page 1, don't show back links
         if ($currentpage > 1) {
            // show << link to go back to page 1
            echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
            // get previous page num
            $prevpage = $currentpage - 1;
            // show < link to go back to 1 page
            echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
         } // end if 
         // loop to show links to range of pages around current page
         for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
            // if it's a valid page number...
            if (($x > 0) && ($x <= $totalpages)) {
               // if we're on current page...
               if ($x == $currentpage) {
                  // 'highlight' it but don't make a link
                  echo " [<b>$x</b>] ";
                  // if not current page...
               } 
               else {
                  // make it a link
                   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
               } // end else
            } // end if 
         } // end for
         // if not on last page, show forward and last page links        
         if ($currentpage != $totalpages) {
            // get next page
            $nextpage = $currentpage + 1;
            // echo forward link for next page 
            echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
            // echo forward link for lastpage
            echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
         } // end if
         /****** end build pagination links ******/
         echo"</div>";
         while($row = mysqli_fetch_array($sql)){
            $data = $row['image'];
            $file = substr($data, strpos($data, "/") + 1);
            echo "<div class='featuredproduct'>",
            "<a class='featuredlink' href='products.php?product=" . $row['id'] . "'>",
            "<div class='productimage'>",
            "<img class='featuredproductimage' src='$file' alt='{$row['name']} Image' 
              />",
            "</div>",
            "<div class='featuredproductname'>{$row['name']}</div>",
            "<div class='featuredproductprice'>&pound{$row['price']}</div>",
            "</a>",
            "</div>";
         }
      }
   }

Upvotes: 0

Views: 1055

Answers (1)

Nawed Khan
Nawed Khan

Reputation: 4391

When going to next and/or previous pages you are not passing the brand.

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";

should be

echo " <a href='{$_SERVER['PHP_SELF']}?brand=$brand&currentpage=1'><<</a> ";

Upvotes: 2

Related Questions