Current location: Hot Scripts Forums » Programming Languages » PHP » Paginating mysql results.


Paginating mysql results.

Reply
  #1 (permalink)  
Old 12-09-03, 10:15 PM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Paginating mysql results.

ok, so i've tried everything known to me, plus the million tutorials out there that dont work, to try and paginate my mysql results. If anyone could point my to a simple tutorial that works, or write up a simple code, i'd be so greatful.

Im pulling the data from a table called "eight" and using $perpage as my limitor. Any help guys?

Thanks.
Reply With Quote
  #2 (permalink)  
Old 12-10-03, 05:40 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by simone
ok, so i've tried everything known to me, plus the million tutorials out there that dont work, to try and paginate my mysql results. If anyone could point my to a simple tutorial that works, or write up a simple code, i'd be so greatful.

Im pulling the data from a table called "eight" and using $perpage as my limitor. Any help guys?

Thanks.
Hi there. I couldn't ignore this because that was exactly what I was thinking about. I've been writing a blog script using my leisure time.

Anyways, first off, I thought PEAR's DB class has pager() or something similar that does all the work for you.

If you are not interested, then here's more or less what I did:

Code:
for ($i = 0; $i < $nav_page_num; $i++) {
	// Each link's Start Post will be higher by $per_page.
	$get_sp = (1 + $per_page * $i);
	// "You're Here" indicator. No anchor.
	if ($per_page_start == $get_sp) {
		echo '<strong>'.($i+1).'</strong> &nbsp;';
	}
	else {
		echo '<a href="'.$_SERVER['PHP_SELF'].'?sp='.$get_sp.'">'.($i+1).'</a> &nbsp;';
	}
}
Vars explained:
$nav_page_num = ceil(mysql_num_rows($YOUR_RESULT) / $per_page);
$per_page_start = (isset($_GET['sp']) ? $_GET['sp'] : 1);
$get_sp: starting (Nth) post parameter to be passed onto $_GET['sp'].

Of course, you need to add a query function or whatever you need before this.

I'm not a pro-programmer so I'm not sure how good it may sound to some people, but I hope this at least gives you the idea.

Good night,
Reply With Quote
  #3 (permalink)  
Old 12-10-03, 07:12 AM
hyjacked hyjacked is offline
Wannabe Coder
 
Join Date: Nov 2003
Location: New Brunswick, Canada
Posts: 174
Thanks: 0
Thanked 0 Times in 0 Posts
Just out of curiosity, what do you mean by "paginate"? To break up the results over several pages?
__________________
hyjacked
Reply With Quote
  #4 (permalink)  
Old 12-10-03, 02:57 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Helloe there,

Quote:
Originally Posted by hyjacked
Just out of curiosity, what do you mean by "paginate"? To break up the results over several pages?
You are right - at least, I assumed so...

Btw, re the PEAR DB class, I found that it was not a function in DB class, but DB_Pager class. You can find more details at http://pear.php.net/package/DB_Pager .

Cheers,
Reply With Quote
  #5 (permalink)  
Old 12-11-03, 09:35 PM
EasyWebDev EasyWebDev is offline
New Member
 
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
phpBB has an excellent pagination function, in functions.php, take a look near the end of search.php to see it in use. Shouldnt take too much work to adapt it to your needs.
Reply With Quote
  #6 (permalink)  
Old 12-24-03, 12:06 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
This one works

Quote:
Originally Posted by simone
ok, so i've tried everything known to me, plus the million tutorials out there that dont work, to try and paginate my mysql results. If anyone could point my to a simple tutorial that works, or write up a simple code, i'd be so greatful.

Im pulling the data from a table called "eight" and using $perpage as my limitor. Any help guys?

Thanks.
This came from a tutorial I found. Had to make some changes to the original code, but it works for me just fine.

PHP Code:

$page $_GET['page'];
 
Add your db connection here  
$limit          
5;      // Change to how many results you want per page          
    
$query_count    "SELECT * FROM table_name";     
    
$result_count   mysql_query($query_count);  
       
    
$totalrows      mysql_num_rows($result_count); 
    if(empty(
$page)){ 
        
$page 1
    } 
         
    
$limitvalue $page $limit - ($limit); 
 
$query = ("your regular query here LIMIT $limitvalue$limit");
$result mysql_query($query); 
$num=mysql_num_rows($result);
if(
mysql_num_rows($result) == 0){ 
        echo(
"Nothing to Display!"); 
    } 

Do 
your display of data here
 
if($page != 1){ 
        
$pageprev $page-1
         
        echo(
"<a href=\"$PHP_SELF?page=$pageprev\">PREV ".$limit."</a>&nbsp;&nbsp;&nbsp;"); 
    }else{ 
        echo(
"PREV&nbsp;" .$limit."&nbsp;&nbsp;&nbsp;"); 
    } 
    
$numofpages $totalrows $limit
     
    for(
$i 1$i <= $numofpages$i++){ 
        if(
$i == $page){ 
            echo(
$i."&nbsp;"); 
        }else{ 
            echo(
"<a href=\"$PHP_SELF?page=$i\">$i</a>&nbsp;"); 
        } 
    } 

    if((
$totalrows $limit) != 0){ 
        if(
$i == $page){ 
            echo(
$i."&nbsp;"); 
        }else{ 
            echo(
"<a href=\"$PHP_SELF?page=$i\">$i</a>&nbsp;"); 
        } 
    } 
    if((
$totalrows - ($limit $page)) >=0){ 
        
$pagenext $page+1
          
        echo(
"&nbsp;&nbsp;&nbsp;<a href=\"$PHP_SELF?page=$pagenext\">NEXT ".$limit."</a>"); 
    }else{ 
        echo(
"&nbsp;&nbsp;&nbsp;NEXT&nbsp;" .$limit); 
    } 
     
    
mysql_free_result($result); 
Hope that helps you out.
Reply With Quote
  #7 (permalink)  
Old 12-26-03, 02:25 PM
clintre clintre is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
inserting images into mysql coatsey PHP 3 12-01-03 07:04 PM
great product for dumping/recovering MySQL databases Dave Brown General Advertisements 1 10-03-03 07:40 AM
Which way to display results from mysql Peter_web PHP 2 09-13-03 05:39 PM


All times are GMT -5. The time now is 06:11 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.