hina
hina

Reputation: 15

pagination using php

in the following code , i am trying to display 10 products detail on one page and then other products detail on next page. the problem is when i am clicking the page 2 link in the bottom , it gives error message that " please select the category". please help me where is the problem in my code.

 $SQLstring = "SELECT product_id FROM products LIMIT $offset,$rowsperpage";    
echo "<table border='1' width=500 ><tr><th>Product ID</th>tr>"; 

    while ($Row = mysqli_fetch_assoc($QueryResult))
             {
              $productid = $Row['product_id'];
         echo "<tr><td>$productid</td></tr>";
 }

    echo "</table>"; 
    $range = 3;
   if ($currentpage > 1)
     {  
    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";   
    $prevpage = $currentpage - 1;   
    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
    } 
  for ($x = ($currentpage - $range);$x < (($currentpage + $range) + 1); $x++) 
    {   
     if (($x > 0) && ($x <= $totalpages)) 
    {
    if ($x == $currentpage) 
   {
   echo " [<b>$x</b>] ";  
   } 
   else 
    {     
  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";      
     }}}                

    mysqli_close($DBConnect);
}

Upvotes: 0

Views: 411

Answers (3)

davidethell
davidethell

Reputation: 12038

Your generated links don't include the category. So in all your links make sure to include categor=$category like this:

echo " <a href='{$_SERVER['PHP_SELF']}?categor=$category&currentpage=...

EDIT: Then, as others correctly caught, you need to change $_POST to $_REQUEST which I've changed in your code example.

Hope that helps.

Upvotes: 1

David Barker
David Barker

Reputation: 14620

I think I see the problem...

It doesn't look like you are passing the category into your next page, maybe store the $_POST['categor'] as a session variable, and then check against that instead of a $_POST.

Alternatively, append the category as well as the next page number to the url

$cat = isset($_REQUEST['categor']) ? $_REQUEST['categor'] : "none";

if ($currentpage != $totalpages) {   
    $nextpage = $currentpage + 1;    
    echo " <a href='{$_SERVER['PHP_SELF']}?cat=$cat&currentpage=$nextpage'>></a> ";  
    echo " <a href='{$_SERVER['PHP_SELF']}?cat=$cat&currentpage=$totalpages'>>></a> ";
}

Upvotes: 1

Tokk
Tokk

Reputation: 4502

Your Problem is this:

$category=$_POST["categor"];

After someone clicked on the link, $_POST["categor"] will be empty. Try $_REQUEST["categor"] and create the links like this:

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

or

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

Doing this you can use POST and GET.

Upvotes: 2

Related Questions