i want to put previous & next on pagination. please help!

Discussion in 'PHP' started by Kyriakos, Apr 21, 2008.

  1. #1
    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 .= "&nbsp;<a href=\"products.php?pg=" . $i . "\">$i</a>";
    }
    }
    Echo "" . $Nav;
    ?>
    
    PHP:
    can anyone give a litle help?

    thanks in advance.
     
    Kyriakos, Apr 21, 2008 IP
  2. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #2
    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 .= "&nbsp;<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:
     
    xrvel, Apr 21, 2008 IP
  3. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    if i want to add one more variable? example:
    <a href="products.php?pg=%s&category=".$category)."">Next</a>
    HTML:
     
    Kyriakos, Apr 21, 2008 IP
  4. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #4
    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 .= "&nbsp;<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:
     
    xrvel, Apr 21, 2008 IP