scriptygoddess

09 Feb, 2003

Add "next" and "previous" buttons to data output pages

Posted by: Jennifer In: Lessons learned

(another non-blog post)
Problem: Let's say you're displaying rows of data from your database, and there's just too many rows for one page. You'd like to show X number of rows of data per page, and then have "next"/"previous" buttons to navigate through the list. Let's also say you want to be able to sort all this data by certain fields, and have that "sort" remembered as you go from page to page (clicking the "next" "previous" buttons).

Solution: (If there's another way to do this without the double query – please let me know)…

<?
//Set this number to the max you want to display per page…
$numToDisplay = 20;
if (isset($_REQUEST['nxtgrp'])) {
$nxtgrp = $_REQUEST['nxtgrp'];
$urlnextgrp = "nxtgrp=".$_REQUEST['nxtgrp'];
$limitTo = "LIMIT ".$_REQUEST['nxtgrp'].",".$numToDisplay;
} else {
$nxtgrp = 0;
$urlnextgrp = "nxtgrp=0";
$limitTo = "LIMIT 0,".$numToDisplay;
}
$sort = "";
$sortby = "";
if (isset($_REQUEST['sortby'])) {
$sort = "ORDER BY ".$_REQUEST['sortby'];
$sortby = "sortby=".$_REQUEST['sortby'];
}
//////————–////
leaving out the code to connect to the database, and select the appropriate table
/////————-/////

$getCount = "SELECT count(*) as count FROM yourTable";
$getCountResult = mysql_query($getCount);
$getCountRow = mysql_fetch_array($getCountResult);
$totalRows = $getCountRow['count'];
?>

<p>
<? if ($nxtgrp > 0) {?>
[ <a href="<? echo $_SERVER['PHP_SELF']; ?>?nxtgrp=<? echo $nxtgrp-$numToDisplay; ?>&<? echo $sortby; ?>"><<previous</a>
]
<? } else {
echo "<font color=\"#999999\">[ <<previous ]</font>";
} ?>
<? echo $nxtgrp+1; ?> –
<? if ($nxtgrp+$numToDisplay > $totalRows) {
echo $totalRows;
} else {
echo $nxtgrp+$numToDisplay;
}
?>
(of <? echo $totalRows; ?>)
<? if (($nxtgrp+$numToDisplay) < $totalRows) { ?>
[ <a href="<? echo $_SERVER['PHP_SELF']; ?>?nxtgrp=<? echo $nxtgrp+$numToDisplay; ?>&<? echo $sortby; ?>">next>></a>
]
<? } else {
echo "<font color=\"#999999\">[ next>> ]</font>";
} ?>
</p>

Then, lets say you have the "header title" in the rows linked to be the one to sort by… so if you have a list of users, and user's data, and you want to be able to have people click on "USERNAME" to sort by that column… do this ("username" is the actual name of the field in the database):

<a href="<? echo $PHP_SELF ?>?sortby=username&<? echo $urlnextgrp; ?>">Username</a>

After you finish your header row, then you do the query for the actual data for this page:

$sql = sprintf("SELECT username, password, access FROM yourTable %s %s", $sort, $limitTo);
$r = mysql_query($sql);
$num = mysql_num_rows($r);
for ($i=0; $i < $num; $i++) {
$row = mysql_fetch_array($r);
<tr>
<td><? echo $row['username']; ?></td>
etc…
</tr>
<? } ?>

So just to put a "face" to all this…Here's your data table kinda looks: (note this is NOT a functioning data display… just wanted to show you what the table that I was working with sort of looks like):

[<<previous] 1-2 (of 5) [next>>]

Username Password Access
Joe superjoe admin
Jim irule baseuser

2 Responses to "Add "next" and "previous" buttons to data output pages"

1 | Beate

May 20th, 2003 at 5:11 am

Avatar

cool work, helped me for my little blog-software. thank you.

2 | Russell Conder

September 28th, 2003 at 12:03 am

Avatar

Hi Jennifer.
Great code.
has anyone asked about a page of results where we can click (say) the 5th item of 10 on a page for more info – do another query and use get_row – but maintain what the 4th and 6th items were ? The table has no ID, results are random. In theory, I get a result page, click to a more detailed page on that row – and would like to have a 'previous record' and 'next record' option without backing up to the original query.
I'm 1/2 way there, but purely by fluke.
I have the next record functioning okay, but the previous record is no-go and I don't understand why.
I'll post the 2 pages here:
http://www.waiheke.co.nz/what.the/heck/
okay ? if you would care to take a look.
I'd be extremely grateful !!!!! and I don't expect a cure. Everything I've read on the web says it's not possible, and the more I try and understand arrays and what to do with them, and copy code from examples and sit and stare at error messages, the more determined I get to resolve it *grin*
..
Okay, thanks for your time.
Best regards
Russell

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements