[thelist] php/mysql previous next function

Martin Kuplens-Ewart martin at takingitglobal.org
Wed Mar 27 13:41:01 CST 2002


I just built something that does this the other day...

<tip type="smart prev/next page linking for data listings">
We must first define how many results we want to see per page.

<?
$rpp = 25;
?>

We next check the page we're on... If there is no $page defined, or if
we are on the first $page, then we want to grab the first $rpp results.
Otherwise, we want to take the number of already seen records ($offset *
$rpp). This will produce the following: page 1: 0...24, page 2: 25...49,
page 3: 50...74, etc. [see how it works]?

<?
if(!$page || $page == 1){
	$start = 0;
}else{
	$offset = $page-1;
	$start = ($offset * $rpp);
}
?>

Next, we pul our data, using $start to set the number of records that we
want to see.

<?
$query = mysql_query("SELECT * FROM $db.$table LIMIT $start,25");
?>

Spit out your data, using mysql_fetch_array($query), etc, then after it,
do the following:
Check whether the $page is greater than 1... If so, then we know that we
can go back a page. And set the prevpage variable to do so, while
setting the count for the last result record currently displayed.

Next, check if there are any records to be found in the database after
the last one being displayed. If so, and if $page is unset, then the
current page is 1 and we must link to page 2. Otherwise, link to the
page after the current $page.

<?
if($page > 1){
	$prevpage = $page - 1;
}
$currentend = $start + 25;
if(mysql_num_rows(mysql_query("SELECT * FROM $db.$table))>$currentend)
{
	if(!$page){
		$nextpage = 2;
	}else{
		$nextpage = $page + 1;
	}
}
?>

Now that we have set the $prevpage and $nextpage variables... We can
have the code check to see if they exist... If so, we must add at least
a previous page, or a next page link.

<?
if ($prevpage || $nextpage) {
	if($prevpage){
		echo ("<a href=\"?page=".$prevpage."\">Previous
Page</a>");
	}else{
		echo ("");
	}
	if($nextpage){
		echo ("<a href=\"?page=".$nextpage."\">Previous
Page</a>");
	}else{
		echo ("");
	}
}
?>

I hope that skeleton guide helps... :)

</tip>

-----Original Message-----
From: thelist-admin at lists.evolt.org
[mailto:thelist-admin at lists.evolt.org] On Behalf Of kris burford
[midtempo]
Sent: March 27, 2002 2:20 PM
To: thelist at lists.evolt.org
Subject: [thelist] php/mysql previous next function


hi,

i'm trying to do a simple previous, next structure to display a large
list of returned fields from a mysql database, plus displaying numbers
of results pages a la google.

can someone point me to a decent tutorial where i can learn how to
achieve this? you'll have guessed by now that i'm not the most
proficient programmer around... ; )

tia

kris
--
For unsubscribe and other options, including
the Tip Harvester and archive of thelist go to: http://lists.evolt.org
Workers of the Web, evolt !






More information about the thelist mailing list