Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more  

Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Web Programming > Databases - MySQL, SQL, Oracle, Access and others
User Name
Password

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-21-2005, 03:47 PM   #1 (permalink)
angie
Member
 
Join Date: Jul 2005
Location: Nebraska
Posts: 33
Default MySQL/PHP - Limit number of results per page

What is a good way of taking say 50 record results and limiting it to 10 per page with pagination next button etc...
angie is offline   Reply With Quote
Sponsored Links
Old 09-05-2005, 11:20 AM   #2 (permalink)
sandman
Junior Member
 
Join Date: Jul 2005
Posts: 36
Default

Hi,

There are several ways to accomplish this. Here is some code you may modify to fit your own site:
PHP Code:
<? 
//REMEMBER TO CONNECT TO DATABASE! 

//**EDIT TO YOUR TABLE NAME, ECT. 

$t = mysql_query(
"SELECT * FROM `table` WHERE `cat` = '".addslashes($_GET['cat'])."'"); 
  if(!$t) die(mysql_error()); 
    
$a                  = mysql_fetch_object($t); 
$total_items      = mysql_num_rows($t); 
$limit            = $_GET
['limit']
$type             = $_GET
['type']
$page             = $_GET
['page']

//set default if: $limit is empty, non numerical, less than 10, greater than 50 
if((!$limit)  || (is_numeric($limit) == false) || ($limit < 10) || ($limit > 50)) 
{ 
     $limit = 10; //default 
} 
//set default if: $page is empty, non numerical, less than zero, greater than total available 
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) 
{ 
      $page = 1; //default 
} 

//calcuate total pages 
$total_pages     = ceil($total_items / $limit); 
$set_limit          = $page * $limit - ($limit); 

//query: **EDIT TO YOUR TABLE NAME, ECT. 

$q = mysql_query(
"SELECT * FROM `table` WHERE `cat` = '".addslashes($_GET['cat'])."' LIMIT $set_limit, $limit"); 
  if(!$q) die(mysql_error()); 
     $err = mysql_num_rows($q); 
       if($err == 0) die(
"No matches met your criteria."); 

//Results per page: **EDIT LINK PATH** 
echo("   
<a href=www.yoursite.com/stuff/script.php?cat=$cat&amp;limit=10&amp;page=1>10</a> | 
<a href=www.yoursite.com/stuff/script.php?cat=$cat&amp;limit=25&amp;page=1>25</a> | 
<a href=www.yoursite.com/stuff/script.php?cat=$cat&amp;limit=50&amp;page=1>50</a>"
); 

//show data matching query: 
while($code = mysql_fetch_object($q)) 
{ 
     echo(
"item: ".results->title."<BR>"); 
} 

$cat = urlencode($cat); //makes browser friendly 

//prev. page: **EDIT LINK PATH** 

$prev_page = $page - 1; 

if($prev_page >= 1) 
{ 
  echo(
"<b>&lt;&lt;</b> <a href=http://www.yoursite.com/stuff/script.php?cat=$cat&amp;limit=$limit&amp;page=$prev_page><b>Prev.</b></a>"); 


//Display middle pages: **EDIT LINK PATH** 

for($a = 1; $a <= $total_pages; $a++) 

   if($a == $page) { 
      echo("
<b> $a</b> | "); //no link 
     
} else { 
  echo("
  <a href=http://www.yoursite.com/stuff/script.php?cat=$cat&amp;limit=$limit&amp;page=$a> $a </a> | "); 
     } 


//next page: **EDIT THIS LINK PATH** 

$next_page = $page + 1; 
if($next_page <= $total_pages) { 
   echo("
<a href=http://www.yoursite.com/stuff/script.php?cat=$cat&amp;limit=$limit&amp;page=$next_page><b>Next</b></a> &gt; &gt;"); 


//all done 
?>

Enjoy
sandman is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Points Per Thread View: 1.00
Points Per Thread: 11.00
Points Per Reply: 5.00



» Sponsors

» Links

» Affiliates
Web Hosting
Marketing Find
Merchant Select
SiteMap Builder
Host Compare
Dedicated Servers

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 01:10 AM.



LinkBacks Enabled by vBSEO 3.0.0 RC8
Webmaster Forums