|
Posted by Geoff Berrow on 06/25/06 09:12
Message-ID: <129s6m92mm3il12@news.supernews.com> from J Huntley Palmer
contained the following:
>Anyone have an efficient solutions for pagination? I have to display a
>number of items but would like to paginate using only 10 lines of data
>on each page and provide navigation at bottom.
Here's a simple one for a guestbook.
Commenting is left as an exercise for the OP :-)
$records_per_page=10;
$offset=(isset($_GET['offset']))?intval($_GET['offset']) :"";
$next=$offset+$records_per_page;
$previous=(isset($_GET['offset']))?$_GET['offset']-$records_per_page
:"";
$sql = "SELECT COUNT(*) AS numrows FROM {$prefix}_guestbook
where status=1";
//echo $sql;
$result = mysql_query($sql) or die("could not run query");
$rows=mysql_fetch_array($result)or die("could not fetch
result");
$rc = $rows['numrows'];
if(!isset($_GET['offset'])||$_GET['offset']==0){
$end=($rc<=$records_per_page)?$rc:$records_per_page;
echo ("<h3>Showing rows 0 to $end out of $rc</h3>");
$query = mysql_query("SELECT * FROM {$prefix}_guestbook where
status=1 ORDER BY `date` DESC LIMIT 0,$records_per_page");
}
else{
$r1=$_GET['offset']+1;
if($next>$rc){$next=$rc;}
echo ("<h3>Showing $r1 to $next out of " . $rc."</h3>");
$sql="SELECT * FROM {$prefix}_guestbook where status=1 ORDER BY
`date` DESC LIMIT $offset,$records_per_page";
$query = mysql_query($sql);
}
echo "<hr>";
echo '<table>';
while($row = mysql_fetch_array($query)){
?>
<tr>
<td class='label'>Name:</td>
<td><?php echo stripslashes($row['name']);?></td>
</tr>
<tr>
<td class='label'>Location:</td>
<td><?php echo stripslashes($row['location']);?></td>
</tr>
<tr>
<td class='label'>Date:</td>
<td><?php echo date("d/m/Y H:i",$row['date'])?></td>
</tr>
<tr>
<td class='label'>Comments:</td>
<td><?php echo stripslashes(str_replace("
/>",">",nl2br($row['comments'])));?></td>
</tr>
<tr>
<td colspan='2'>
<hr>
</td>
</tr>
<?php
}
echo "</table>";
if($previous > -1 ){
echo "<a
href=\"".$_SERVER['PHP_SELF']."?offset=$previous#records\">previous</a>";
}
if($rc>$next){
echo($previous > -1)?" | ":"";
echo "<a
href=\"".$_SERVER['PHP_SELF']."?offset=$next#records\">next</a>";
}
mysql_free_result($query);
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Navigation:
[Reply to this message]
|