Page 1 of 1

A better way to navigate pages?

Posted: Tue Nov 11, 2003 10:13 pm
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!

Posted: Tue Nov 11, 2003 10:21 pm
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 ;) )

Posted: Wed Nov 12, 2003 2:22 am
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 !