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
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
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)
 



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

 
     

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