Here is some code that I like to use.  I mix real code and psuedo code cause
I'm more used to just using the db class from PHP Lib.

First get a count of how many records there are.

$sql = "SELECT count(*) as c FROM users";
Then read that count into variable $count.

Declare a variable which will contain the number of records per page, this
is a really good idea cause if that number changes later you just have to
change one variable instead of hunting down every instance of that number.

$records_per_page = 10;

Next figure out how many pages you'll have.

$total_pages = ceil( $count / $records_per_page );

This just sets up a page variable which will be past in a "next" and "prev"
link.  The first link to this page may not actually have $page so this just
sets it up.
if ( !isset($page) ) { $page = 1; }

I like to setup a little navigation that has "First | Prev | Next | Last" on
the page with each word being a link or not depending on what page you are
on.  To do this I use the following piece of code.

# this will set up the "First | Prev | " part of our navigation.
if ( $page == 1 ) {
    # if we are on the first page then "First" and "Prev"
    # should not be links.
    $naviagation = "<font color=\"#666666\">First</font> | <font
color=\"#666666\">Prev</font> | ";
} else {
    # we are not on page one so "First" and "Prev" can
    # be links
    $prev_page = $page - 1;
    $navigation = "<a href=\"your_page.php?page=1\">First</a> | <a
href=\"your_page.php?page=$prevpage\">Prev</a> | ";

# this part will set up the rest of our navigation "Next | Last"
if ( $page == $total_pages ) {
    # we are on the last page so "Next" and "Last"
    # should not be links
    $navigation .= "<font color=\"#666666\">Next</font> | <font
} else {
    # we are not on the last page so "Next" and "Last"
    # can be links
    $next_page = $page + 1;
    $navigation .= "<a href=\"your_page.php?page=$next_page\">Next</a> | <a

With your navigation built you can just echo that out any where you want it
to appear.

The final thing to do is your actual query, but first we have to figure out
what the offset will be.

$offset = ( $page - 1 ) * $total_pages;
$sql = "SELECT id, email, name, subject, url, image2, comments, dat
        FROM users
        ORDER BY id DESC
        LIMIT $offset,$records_per_page";

And that's it?  Simple eh?

You may also want to put out something like: echo "$page of $total_pages";


----- Original Message -----
Sent: Thursday, July 12, 2001 3:49 AM
Subject: [PHP-DB] Paging help needed :-(

Hi there Everyone,

I currently have a dedicated Apache server with PHP 4.06, MySQL etc ..
installed and running fine.  Below is alittle peice of code that I would
love an answer to:


$connection = mysql_connect("Localhost","username!!!","password!!!") or
die("Couldn't make a connection.");

$db = mysql_select_db("planet", $connection)
 or die("Couldn't select database.");

$sql = "SELECT id, email, name, subject, url, image2, comments, dat
        FROM users
        ORDER BY id DESC";

$sql_result = mysql_query($sql,$connection)
 or die("Couldn't execute query.");



while ($row = mysql_fetch_array($sql_result)) {
 $email = $row["email"];
 $name = $row["name"];
 $subject = $row["subject"];
 $url = $row["url"];
 $image2 = $row["image2"];
 $comments = $row["comments"];
 $dat = $row["dat"];

$ip = getenv ("REMOTE_ADDR");


then all the HTML and display bits (Example at in the





Now my question is, how do I do paging in PHP?  I need to display 10 entries
per page, and be able to go back and forth with them.  I'm relatively new to
PHP so I would be really grateful for any advice.

Thanks everyone, I appreciate it.

Chris Payne

PHP Database Mailing List (
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to