ProgrammingTalk


Welcome to the ProgrammingTalk forums. We are a programming, coding, and design oriented community dedicated to helping out with all different types of programming questions and languages.

You are currently viewing our board as a guest which gives you very limited access to view discussions and access our other features. In order to gain full access to this board, we require you to register. Registration is absolutely free and very easy to accomplish, simply click the link above to begin. If you have already registered with us, all you have to do is login.

If you have any problems with the registration process or your account login, please don't hesitate to contact Support.
Go Back   ProgrammingTalk > Programming Languages > PHP
Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-27-05, 12:46 PM
toray toray is offline
Newbie Coder
 
Join Date: Mar 2005
Posts: 23
Exclamation php pagination

hello does any1 have a script for php paging from mysql

something like <<prev 1 2 3 Next>>

thanks
Reply With Quote
  #2  
Old 05-27-05, 03:16 PM
Jaffizzle Jaffizzle is offline
Newbie Coder
 
Join Date: May 2005
Posts: 61
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
  #3  
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
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>' ''
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 Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 ProgrammingTalk Questions, Suggestion, 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

Advertisement:

All times are GMT -5. The time now is 04:58 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright © 1998-2008 ProgrammingTalk. All Rights Reserved. A division of iNET Interactive
Related iNET Interactive Sites:
Web Hosting Talk | Hot Scripts | OC Forums | Mac-Forums