Click to See Complete Forum and Search --> : http://localhost/hardware/hardwares.php?category_id=5


mahfooz
10-17-2005, 02:27 AM
hi to all!

i want to search the products from my site. with the following address as the page load the specific category products. for example, category_id=5 relates the cd-rom category.

when page is opened it find the query based on query string and also want to show 10 products per page it works fine but when products exeeds the limit (10), it also creats the total page but when i goes to next page it set the error message. can any body lead me to the right way that what should i do with that script for creating the pages. thanks

here is the page link that presents in the address bar of the browser when page is loaded.

http://localhost/hardware/hardwares.php?category_id=5


also script i use is as follows:

<?php
$sql = mysql_query("SELECT * FROM `products` WHERE ".$_SERVER['QUERY_STRING']);
$numrows = mysql_num_rows($sql);
print "In this category products are : ".$numrows;

if( empty($page) ) {
$page = 0;
}

$limit = 10;
$products = mysql_query("SELECT * FROM `products` WHERE ".$_SERVER['QUERY_STRING']." ORDER BY `product_name` ASC LIMIT $page, $limit ");

while ( $res = mysql_fetch_array($products) ) {
$pages = intval( $numrows / $limit );
if ( $numrows % $limit ) {
$pages++;
}

for ( $i=1; $i <= $pages; $i++ ) {
$newpage = $limit*($i-1);
$next = "<a href=$PHP_SELF?page=$newpage>Page $i</a> &nbsp; \n";
}


?>

ShrineDesigns
10-17-2005, 11:07 AM
you can use SQL_CALC_FOUND_ROWS (MySQL 4, 4.1, 5) in your select statement ex.,SELECT SQL_CALC_FOUND_ROWS * FROM `products` WHERE `category_id` = '{$_GET['category_id']}' ORDER BY `product_name` ASC LIMIT $page, $limitthen right after the query run this querySELECT FOUND_ROWS() that will return the total rows as if the LIMIT was not set

secondly, to calculate the limit use this, also your $page var should default to 1 not 0LIMIT (page_number - 1) * total_per_page, total_per_pagethis should do the trick<?php
$ppg = 10;
$pg = (!isset($_GET['page']) || !is_numeric($_GET['page'])) ? 1 : $_GET['page']);

$result = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM `products` WHERE `category_id` = '{$_GET['category_id']}' ORDER BY `product_name` ASC LIMIT " . ($pg - 1) * $ppg . "," . $ppg;
$total = mysql_result(mysql_query("SELECT FOUND_ROWS()"), 0);
echo "In this category products are : " . $total;

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
// output results
}
mysql_free_result($result);

if($total > $ppg)
{
$nav = array();

for($i = 1; $i <= ceil($total / $ppg); $i++)
{
$nav[] = ($i == $pg) ? "<b>$i</b>" : "<a href=\"{$_SERVER['PHP_SELF']}?category_id={$_GET['category_id']}&amp;page=$i\">$i</a>";
}
echo implode(" &nbsp;\n", $nav);
}
?>