Current location: Hot Scripts Forums » Programming Languages » PHP » php pagination

php pagination

Reply
  #1 (permalink)  
Old 05-27-05, 12:46 PM
toray toray is offline
Newbie Coder
 
Join Date: Mar 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation php pagination

hello does any1 have a script for php paging from mysql

something like <<prev 1 2 3 Next>>

thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-27-05, 03:16 PM
Jaffizzle Jaffizzle is offline
Newbie Coder
 
Join Date: May 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
eeh here ya go i just wrote a script for ya.

PHP Code:
<?php
/* ALOT OF THE CONFIGS CAN BE CHANGED IN THE CONSTRUCTOR */

include ("pagination.inc.php");
/*db stuff goes here */
$pgtn = new pagination
$pgtn->sql="SELECT * FROM some_table";  //sql
$pgtn->per_page=5;  //number of listings per page
$pgtn->constructor($_GET['page'], "page");  //if you wish to change your var..ie [url]www.somesite.com?page=1[/url] to [url]www.somesite.com?id=1[/url]
$limit $pgtn->limit();  //executes query and returns result
while ($row=mysql_fetch_assoc($limit)){  //displays the result
    
echo $row['some_info']."<br>";
}
    
$pgtn->display_pages();  //displays the pagination
?>
heres the class file...
pagination.inc.php

PHP Code:
<?php
//pagination class
class pagination{
var 
$sql;
var 
$start;
var 
$page;
var 
$total_pages;
var 
$pagevar;
var 
$per_page;
var 
$limit_pages;


function 
constructor($getpage$page_var){ //$page = $_GET['page'];
if(empty($getpage))
    
$this->page=1;
else
    
$this->page=$getpage;
if(empty(
$this->pagevar)) $this->pagevar $page_var;
else
    
$this->pagevar="page";
$this->start=$this->page*$this->per_page-$this->per_page;

$result=mysql_query($this->sql) or die(mysql_error());
$total_rows=mysql_num_rows($result);
if(
$total_rows%$this->per_page==0)
    
$this->total_pages $total_rows/$this->per_page;
else
    
$this->total_pages $total_rows/$this->per_page+1;

//$this->limit_pages=$this->page-$this->per_page;
//$this->limit_pages_end=$this->total_pages;
$this->limit_pages=$this->page-5;  //start  pages  start with current page - 5
$this->limit_pages_end=$this->page+5;  //end page  end with current page + 5
}

function 
limit()
{
    
$result=mysql_query($this->sql." LIMIT ".$this->start.", ".$this->per_page);
    return 
$result;
}

function 
display_pages(){
if(
$this->page 1)
    echo 
"<a href=\"".$_SERVER['PHP_SELF']."?".$this->pagevar."=".($this->page-1)."\"><< </a>";
else
    echo 
"<< ";
    
for(
$i $this->limit_pages$i <= $this->limit_pages_end$i++){  //use to limit how many pages displayed
        
if($i 1){
            echo
"";
        }elseif (
$i $this->total_pages){
            echo
"";
        }elseif(
$i == $this->page){
            echo 
$i."&nbsp;";
        }else{
           echo 
"<a href=\"".$_SERVER['PHP_SELF']."?".$this->pagevar."=".$i."\">$i </a>";
        }
    } 
    
if(
$this->page <= $this->total_pages)
    echo 
"<a href=\"".$_SERVER['PHP_SELF']."?".$this->pagevar."=".($this->page+1)."\">>> </a>";
else
    echo 
">> ";
     }
}

?>
sorry about the lack of comments..i hate commenting


but lemme know how it goes!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-27-05, 03:46 PM
wheezy360's Avatar
wheezy360 wheezy360 is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Toronto, ON
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by toray
hello does any1 have a script for php paging from mysql

something like <<prev 1 2 3 Next>>

thanks
This one's a little more compact than the above, if you like..
It relies on the following query string variables: "page" (current page number), and "len" (the max number of records to display per page).

PHP Code:

/*****
This portion will handle calculating which records to get from the db
*****/

// If the query string variable is defined, use that.  Otherwise, default to page 1
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// The number of records to display per page.  Defaults to 25 if not specified
$pagelength = isset($_GET['len']) ? $_GET['len'] : 25;

// The lowerbound record number to start from
$lowerbound = ($page 1) * $pagelength;

$rs mysql_query('SELECT * FROM mytable LIMIT '.$lowerbound.','.$pagelength) or die(mysql_error());


/****
This portion outputs the page numbers so make sure you put it where you want the text to be displayed.  Also note that it's relying on the $pagelength, and $page variables from above so make sure this is same scope level
****/

$numpages ceil(mysql_num_rows(mysql_query('SELECT * FROM mytable'))/$pagelength);

// Output the "Previous" link if you are not on the first page
echo $page '<a href="'.$PHP_SELF.'?page='.($page-1).'&len='.$pagelength.'">Previous</a>' '';

// Loop through the page numbers
// Do not link the page number you are currently viewing
for ($i=1$i <= $numpages$i++) echo $i == $page $i '<a href="'.$PHP_SELF.'?page='.$i.'&len='.$pagelength.'">'.$i.'</a>';

// Output the "Next" link if you are not on the last page
echo $page $numpages '<a href="'.$PHP_SELF.'?page='.($page+1).'&len='.$pagelength.'">Next</a>' ''
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
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 03:22 AM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 11:17 AM
PHP Downside--Solutions? Amulet PHP 10 07-15-05 09:26 AM
php with Apache in windows eDevil PHP 3 08-08-04 01:03 AM
100 Web Templates & 10 PHP Scripts for sale! HostersUK.co.uk General Advertisements 0 01-10-04 01:31 AM


All times are GMT -5. The time now is 12:40 AM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.