Thread: php pagination
View Single Post
  #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!
Reply With Quote