MySQL/PHP - Limit number of results per page - Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Webmaster Tech > Databases - MySQL, SQL, Oracle, Access and others

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-21-2005, 04:47 PM   #1 (permalink)
Junior 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, 12:20 PM   #2 (permalink)
Regular User
 
Join Date: Jul 2005
Posts: 43
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
Old 01-16-2010, 03:49 AM   #3 (permalink)
Junior Member
 
Join Date: Nov 2009
Posts: 32
Default Re: MySQL/PHP - Limit number of results per page

Hi.
Please refer to the url below:
Online Documentation - Developer Express Inc.
I wish it will be helpful.
Regards,
freezea.
freezea is offline   Reply With Quote
Old 01-24-2010, 09:31 AM   #4 (permalink)
Junior Member
 
Join Date: Oct 2009
Posts: 6
Default Re: MySQL/PHP - Limit number of results per page

Page Numbering With PHP And MySQL Results. Do you have a lot of MySQL ... to create page numbers and limit the number of results per page that you have.










_______________
Download Windows 7
Bob Willis is offline   Reply With Quote
Old 01-29-2010, 04:51 PM   #5 (permalink)
Regular User
 
Join Date: Jan 2010
Posts: 23
Default Re: MySQL/PHP - Limit number of results per page

Take 10 records instead of 50. What the point of taking 50 records and showing only 10?
nice123 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

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



» Sponsors

» Links

» Affiliates
Web Hosting
Online Backup Reviews
Marketing Find
Merchant Select
SiteMap Builder
Host Compare

» 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 08:39 PM.


Powered by vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2
Webmaster Forums
Web Hosting | Chicago Web Hosting | Web Hosting