A better way to navigate pages?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

A better way to navigate pages?

Post by 3.14 »

I have a table which shows the first five records:

Code: Select all

<?php
$result = mysql_query("SELECT date, heading FROM messages ORDER BY message_id DESC LIMIT 0, 5");
$num_rows = mysql_num_rows($result); // the number of records returned
?>
I want to put previous and next buttons which will show the previous five and next five respectively. I was thinking about adding another page which called the same sql query but had a limit of 5, 10 etc but then thought about all those extra pages. Also, I'd have to create a separate page for every set of five. What if there are 100 messages? I have to create 20 pages. No thanks!!!

So, there has to be some fancy php variable page thingie I can do to reload the page with the new set of data upon clicking the next or previous button. Any clues on where I can start?

Thanks!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

this might give you a clue

Code: Select all

<?php

$dbConn = getDBConnection(); // whatever this might be

$entriesPerPage = 5;
/** if no startpage parameter has been passed use 0 without a warning */
$offset = (int)@$_GET['startpage'] * $entriesPerPage;

$query = 'SELECT count(*) FROM messages';
$result = mysql_query($query, $dbConn);
$totalNumRecords = array_pop(mysql_fetch_row($result));

$query = 'SELECT date, heading FROM messages ORDER BY message_id DESC LIMIT '.$offset.','.$entriesPerPage;
$result = mysql_query($query, $dbConn);

/** .... */

?>
(script tested not even by compiler ;) )
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

Code: Select all

<?php
$query = 'SELECT count(*) FROM messages';
$result = mysql_query($query, $dbConn);
$totalNumRecords = array_pop(mysql_fetch_row($result));
?>
will you please explain why you have done these steps !
Post Reply