Adding pagination to existing product pages

Hi there, I’ve been trying to implement pagination to an existing script that displays products, but at present only displays next and back buttons. The number of products is now increasing and we would like to show the number of pages of results but the tutorials I’ve been going through seem to use a very different way of displaying the results to the script we currently have. Does anyone have any suggestions how I could add pagination to this form…

    // Build Pagination

$ByPage = ($prod_rows * $prod_cols);

$qnav = "SELECT devbg_products., devbg_categories., devbg_subcategories.* FROM devbg_products
LEFT JOIN devbg_categories ON devbg_products.ItemCategory = devbg_categories.CategoryID
LEFT JOIN devbg_subcategories ON devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID
WHERE devbg_products.ItemCategory = " . sql($cgid);

    $rnav = mysql_query($qnav) or die(mysql_error());
    $rows = mysql_num_rows($rnav);

    if($rows > $ByPage)
    {
            $pages = ceil ($rows / $ByPage);
        

            echo "<br>\n";
            echo "<table align='center'>\n";
            echo "<tr>\n";
            echo "<td align=center><font face=verdana size=2>";
            echo "<form method='POST' action='ShowCategory.php'>\n";
            echo "<input type='hidden' name='Start' value='" . ($Start - $ByPage) . "'>\n";
            echo "<input type='hidden' name='CategoryID' value='" . $_REQUEST["CategoryID"] . "'>\n";
            echo "<input type='hidden' name='SubcategoryID' value='" . $_REQUEST["SubcategoryID"] . "'>\n";

            if (($Start - $ByPage) <> -$ByPage)
            {
                    echo "<input class='sub' type='submit' value='<<< Back' name='btnBack'>\n";
                    echo "</form>\n";
                    echo "</td>\n";
            }

            echo "<td>\n";
            echo "<form method='POST' action='ShowCategory.php'>\n";
            echo "<input type='hidden' name='Start' value='" . ($Start + $ByPage) . "'>\n";
            echo "<input type='hidden' name='CategoryID' value='" . $_REQUEST["CategoryID"] . "'>\n";
            echo "<input type='hidden' name='SubcategoryID' value='" . $_REQUEST["SubcategoryID"] . "'>\n";

        
        
            if ($Start + $ByPage < $pages * $ByPage)
            {
                    echo "<input class='sub' type='submit' value='Next >>>' name='btnNext'>";
            }


            echo "</form>\n";
            echo "</td>\n";
            echo "</tr>\n";
            echo "</table><br><br>\n";
    }

I’ve put together how I more or less do it:

[php]

<?php if(!isset($_GET['p'])){ $page = 1; } else { $page = $_GET['p']; } // Define the number of results per page $max_results = 10; // Figure out the limit for the query based $from = (($page * $max_results) - $max_results); $q = mysql_query("SELECT devbg_products.*, devbg_categories.*, devbg_subcategories.* FROM devbg_products LEFT JOIN devbg_categories ON devbg_products.ItemCategory = devbg_categories.CategoryID LEFT JOIN devbg_subcategories ON devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID WHERE devbg_products.ItemCategory = " . sql($cgid) ." LIMIT $from, $max_results") or die(mysql_error()); while($r = mysql_fetch_object($q)){ //your output here } //this one will need tweaking $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM table"),0); // Figure out the total number of clientspages. Always round up using ceil() $total_pages = ceil($total_results / $max_results); echo "
\n"; // Build Previous Link if($page > 1){ $prev = ($page - 1); echo "Previous\n "; } for($i = 1; $i <= $total_pages; $i++){ if($total_pages > 1){ if(($page) == $i){ echo "$i\n "; } else { echo "$i\n "; } } } // Build Next Link if($page < $total_pages){ $next = ($page + 1); echo "Next\n"; } echo "
\n" ?>

[/php]

its mostly fit into your code this line will need adjust though its purpose it to get the number of records in total
[php]$total_results = mysql_result(mysql_query(“SELECT COUNT(*) as Num FROM table”),0);[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service