TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-05-2007, 10:41 PM   #1 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default Pagination

Since i seen someone request one. I thought i would write a quick one

You can view demo HERE

Firstly connect to your database using the following

PHP Code:
mysql_connect("host","dbuser","dbpass") or  die("ERROR:".mysql_error());
mysql_select_db("dbname") or die("ERROR DB:".mysql_error()); 
Now we can start sorting out things like setting how many values we want on each page.

PHP Code:
$max 25
Now we have done that we need to get the current page. If there is no page set, we must set it to page 1.

PHP Code:
$p $_GET['p'];
if(empty(
$p))
{
   
$p 1;

Ok now we want to set the limit of values of each page ready for the MySQL query.

PHP Code:

$limits 
= ($p 1) * $max
Now we can use the query to select from the database with limits and maximum on each page.

PHP Code:
$sql mysql_query("SELECT * FROM table LIMIT $limits,$max") or die(mysql_error()); 
Now we want to work out just how many results we have all together.

PHP Code:
$totalres =  mysql_query("SELECT * FROM table");
$count mysql_num_rows($totalres); 
There are easier ways to work that out, but i am using that method for now

Ok now we have the total number of values we now need to work out just how many pages thats going to use.

PHP Code:
$totalpages ceil($count $max); //Returns the next highest integer value by rounding up value if necessary. 
Now we can start constructing the table and display the results

PHP Code:

echo "<table border=1 width=500><tr><td><b>Title</b></td><td><b>Type</b></tr>";
 
   while(
$r mysql_fetch_array($sql))
   {
    echo 
"<tr><td>$r[name]</td><td>$r[type]</td></tr>";
      
   }
    
echo 
"</table>"
Now its time to show all the links for all the pages to go from results to results

PHP Code:
for($i 1$i <= $totalpages$i++)
{
  echo 
"<a href=database.php?p=$i'> $i </a>|";    

This was a simple pagination, of course you can add NEXT > PREVIOUS and so on.

Full Code:

PHP Code:
<?php

mysql_connect
("host","dbuser","dbpass") or  die("ERROR:".mysql_error());
mysql_select_db("dbname") or die("ERROR DB:".mysql_error());  

$max 25//amount of articles per page. change to what to want
$p $_GET['p'];
if(empty(
$p))
{
   
$p 1;
}

$limits = ($p 1) * $max;

$sql mysql_query("SELECT * FROM table LIMIT $limits,$max") or die(mysql_error());
  
      
//the total rows in the table
 
$totalres =  mysql_query("SELECT * FROM table");
$count mysql_num_rows($totalres);

$totalpages ceil($count $max);
 
      
//the table

echo "<table border=1 width=500><tr><td><b>Title</b></td><td><b>Type</b></tr>";
 
    while(
$r mysql_fetch_array($sql))
    {
       echo 
"<tr><td>$r[name]</td><td>$r[type]</td></tr>";
      
    }
    
echo 
"</table>";
    
for(
$i 1$i <= $totalpages$i++)
{
    
   echo 
"<a href=dvds.php?p=$i'> $i </a>|";
    
 }

?>
As request...a google like not exact, but the same concept style buttons on the pagination. I have updated the link code below.

PHP Code:
 $show 10// How many links to show
        
           
echo "<br><br>";
           
                  if(
$p 1// If p > then one then give link to first page
                  
{
                        echo 
"<a href=?p=1> [FIRST] </a>  ";    
                  }
                  else{ 
// else show nothing
                        
echo "";
                  }
                  if(
$p != 1){ // if p aint equal to 1 then show previous text
                  
                          
$previous $p-1;
                          echo 
"<a href=?p=$previous> [ PREVIOUS ] </a>";
                        
                  }
                  else{ 
//else show nothing
                          
echo "";
                  } 
                  for(
$i =1$i <= $show$i++) // show ($show) links
                  
{
                     
                        if(
$p $totalpages){ // if p is greater then totalpages then display nothing
                            
echo "";
                        }
                        else if(
$_GET["p"] == $p){ //if p is equal to the current loop value then dont display that value as link
                            
echo $p ";
                        }
                        else{
                            echo 
" <a href=?p=$p> ( $p ) </a>"// else display the rest as links
                        
}
            
                        
$p++; //increment $p  
                  
}
                    echo 
"....."// display dots
                    
                   
if($_GET["p"] == $totalpages){// if page is equal to totalpages then  dont display the last page at the end of links
                            
echo "";
                   }
                   else 
// else display the last page link after other ones
                   
{
                        echo 
"<a href=?p=$totalpages> ( $totalpages ) </a>"
                   }
                   if(
$_GET["p"] < $totalpages)// if p is less then total pages then show next link
                   
{
                        
$next $_GET["p"] + 1;
                        echo 
"<a href=?p=$next> [ NEXT >] </a>";    
                   }
                  
          echo 
"<br><br>"
__________________
www.jooney.co.uk - the online portfolio

Last edited by Rendair : 12-06-2007 at 04:54 PM.
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following 5 Users Say Thank You to Rendair For This Useful Post:
bdm (12-06-2007), Gurnk (12-06-2007), Karl (12-06-2007), Wildhoney (12-05-2007), WinSrev (12-06-2007)
Old 12-06-2007, 12:01 PM   #2 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Nice article.

But why is there an ' at the end of each page number?
bdm is offline  
Reply With Quote
Old 12-06-2007, 12:04 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Damnit, you beat me to it! ;)
I was going to write an article about pagination aswell after I read some post in the "Top 10 scripts a programmer should code" or whatever that topic was called ;)

Nice article :)
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
bdm (12-06-2007)
Old 12-06-2007, 12:20 PM   #4 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

Quote:
Originally Posted by gcbdm View Post
Nice article.

But why is there an ' at the end of each page number?
Sorry that was my error it works either way lol take it out or leave it. Doesnt matter

Sorry Tanax haha couldn't help myself
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-06-2007, 01:28 PM   #5 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Damnit, you beat me to it! ;)
I was going to write an article about pagination aswell after I read some post in the "Top 10 scripts a programmer should code" or whatever that topic was called ;)
Why not continue writing your article to show the approach you took?
bdm is offline  
Reply With Quote
Old 12-06-2007, 01:30 PM   #6 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Well, this pagination will just keeping going, why not make it so that theres like
1,2 .. 8, 9 a bit like what Google does?
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 12-06-2007, 02:15 PM   #7 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Nice article, thanks for sharing. My only gripe would be that the code is not very flexible. You should wrap it in a class which can then be used in your projects.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-06-2007, 03:00 PM   #8 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

Well as i did say it was a quick one, but ill happily improve on it and add to it make it a bit more friendly.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-06-2007, 03:12 PM   #9 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

It'd be really awesome if you could do the 1,2 .. 8, 9 thing
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 12-06-2007, 04:55 PM   #10 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

Quote:
Originally Posted by WinSrev View Post
It'd be really awesome if you could do the 1,2 .. 8, 9 thing

I have updated the code above well added an extra bit just replace the link code.

Its not exactly like the google one, but it doest clog the page with lots of links.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-06-2007, 05:33 PM   #11 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

It is what it is: a nice and simple pagination class. If you were to get all complex about it then I'd be inclined to take the approach of passing in the format you wish the pagination to be returned as. Such as:

php Code:
$pObj = new Pagination(new Pagination_Ellipsis());

It would also be more programmer-friendly if you could pass in the total amount of records into the class, and how many you would like per page - and if you wanted to take that further then which column you want the items to be ordered by.

In fact, thinking about it, instead of passing in the SQL result yourself, you would specify just the SQL clause and the class would do the rest for you. For example:

php Code:
$pObj   ->  setTable('users')
        ->  setClause('ORDER BY username ASC')
        ->  setPerPage(10);

Unless anybody has a better solution then that's how I would currently do it if I were to write a pagination class, in conjunction with the passing in the pagination type to the class construct.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-06-2007, 06:55 PM   #12 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by gcbdm View Post
Why not continue writing your article to show the approach you took?
Maybe I will write one ;)

But if I would, I would probs want to do pagination by class.
And I have no idea how to do that :| :P
Tanax is offline  
Reply With Quote
Old 12-06-2007, 07:27 PM   #13 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

I shall get on writing a class for it and will either post it up if no one has done so
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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


All times are GMT. The time now is 02:22 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design