/    Sign up×
Community /Pin to ProfileBookmark

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

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, [U][B]category_id=5[/B][/U] 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.

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

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”;
}

?>

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsOct 17.2005 — you can use SQL_CALC_FOUND_ROWS (MySQL 4, 4.1, 5) in your select statement ex.,SELECT SQL_CALC_FOUND_ROWS * FROM <span><code>products</code></span> WHERE <span><code>category_id</code></span> = '{$_GET['category_id']}' ORDER BY <span><code>product_name</code></span> 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[code=php]<?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);
}
?>[/code]
×

Success!

Help @mahfooz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.11,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...