Search
Left Quote    I have not failed. I've just found 10,000 ways that won't work.
- Thomas Alva Edison    
Right Quote
 
[login] | [Register]
 

Pagination

by: bs0d
 

Description:


This code is for you to paginate information from a table in your database. Comments have been made in the code where you should make changes for it to work properly for you.

If you did not arrive to this page from the tutorial, you might want to read the tutorial this code sample is from.




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: ".$code->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
?>


 


No Comments for this page.

You Must be signed in or a member to comment.


Code Stats

Code Stats

38,307 Views
0 Total Comments
4 Rating of 5 ( Votes)

Options

Code Options

· Login to Rate This Code
· Login to Post a Comment
· Read more by this author
Digg This Code! Del.icio.us: Bookmark This Code Reddit: Bookmark This Code! BlinkList: Blink This Code! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Code Spurl: Mark This Code!

Code Samples

Related    

· Database Connection
· Users Online
· Comments Script
· CAPTCHA Image
· IdealMySQL Class for PHP 5.0





"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank