hi guys, i have a pagination for my products and i want to put a previous and a next link. how i can do this? my php code: <?php mysql_connect("localhost","username","password") or die("Unable to connect to SQL server"); mysql_select_db("storedb") or die("Unable to SELECT DB"); $Limit = 6; $result=mysql_query("SELECT * FROM products WHERE cat='".$cat."' AND status=1 ORDER BY pricer") or die(mysql_error()); $NumberOfResults=mysql_num_rows($result); $NumberOfPages=ceil($NumberOfResults/$Limit); $result=mysql_query("SELECT * FROM products WHERE cat='".$cat."' AND status=1 ORDER BY pricer LIMIT " . ($pg-1)*$Limit . ",$Limit") or die(mysql_error()); ?> <table> <?php while($row = mysql_fetch_array($result)) { ?> <tr><td>blablabla</td></tr> <?php } ?> <?php //Create and print the Navigation bar $Nav=""; For($i = 1 ; $i <= $NumberOfPages ; $i++) { If($i == $pg) { ?> <?php $Nav .= "<b>$i</b>"; ?><?php }Else{ ?><?php $Nav .= " <a href=\"products.php?pg=" . $i . "\">$i</a>"; } } Echo "" . $Nav; ?> PHP: can anyone give a litle help? thanks in advance.
I haven't tried your code, but i assume that your previous code works Here's the code. Btw, i cleaned your code a little bit. <?php //Create and print the Navigation bar $Nav=""; // if current page is not 1, draw PREVIOUS link if ($pg > 1 && $NumberOfPages != 0) { printf('<a href="products.php?pg=%s">Previous</a>', ($pg - 1)); } For($i = 1 ; $i <= $NumberOfPages ; $i++) { If($i == $pg) { $Nav .= "<b>$i</b>"; } else { $Nav .= " <a href=\"products.php?pg=" . $i . "\">$i</a>"; } } Echo "" . $Nav; // if current page less than max pages, draw NEXT link if ($pg < $NumberOfPages && $NumberOfPages != 0) { printf('<a href="products.php?pg=%s">Next</a>', ($pg + 1)); } ?> PHP:
if i want to add one more variable? example: <a href="products.php?pg=%s&category=".$category)."">Next</a> HTML:
Ok, i replace the printf() with common echo() <?php //Create and print the Navigation bar $Nav=""; $cat = "something";// category // if current page is not 1, draw PREVIOUS link if ($pg > 1 && $NumberOfPages != 0) { echo("<a href=\"products.php?pg=". ($pg - 1) ."&category=$cat\">Previous</a>"); } For($i = 1 ; $i <= $NumberOfPages ; $i++) { If($i == $pg) { $Nav .= "<b>$i</b>"; } else { $Nav .= " <a href=\"products.php?pg=" . $i . "\">$i</a>"; } } Echo "" . $Nav; // if current page less than max pages, draw NEXT link if ($pg < $NumberOfPages && $NumberOfPages != 0) { echo("<a href=\"products.php?pg=". ($pg + 1) ."&category=$cat\">Next</a>"); } ?> PHP: