RE: [PHP-DB] Select a subset?

From: Date: Tue, 16 Jul 2002 08:44:47 +0000
Subject: RE: [PHP-DB] Select a subset?
Groups: php.db 
Request: Send a blank email to php-db+get-20571@lists.php.net to get a copy of this message
> From: Clive Bruton [mailto:clive@indx.co.uk] > Sent: Monday, July 15, 2002 5:01 PM > > How do I get both the number of rows found (5,000) and get a > subset of > the records (ie 0-9, 10-19, 20-29...) so that a user can > browse through > the records rather than getting 5,000 at a time (but still > know that a > total of 5,000 were found). I think you need to do two querys. At least that's how I do it... First SELECT COUNT(*) FROM table and then SELECT whatever FROM table LIMIT 0,10 list.php: <?php $rows_to_list = 10; // Number of rows per page if(isset($_GET['offset'])) // Find out which row to start with $offset = $_GET['offset']; else $offset = 0; $result = mysql_query("SELECT COUNT(*) as numrows FROM table",$db); $row = mysql_fetch_array($result); $num_rows = $row['num_rows']; echo "Displaying rows " . $offset + 1 . " to $rows_to_list (total $num_rows rows)\n"; $result = mysql_query("SELECT * FROM table LIMIT $offset, $rows_to_list",$db); echo "<table border=1>"; echo "<tr><td>Name</td><td>Position</tr>\n"; while ($myrow = mysql_fetch_row($result)) { printf("<tr><td>%s %s</td><td>%s</tr>\n", $myrow[1], $myrow[2], $myrow[3]); } echo "</table>\n"; ?> <a href="list.php?offset=<?php echo $offset - $rows_to_list; ?>">Previous <?php echo $rows_to_list ?></a><br> <a href="list.php?offset=<?php echo $offset + $rows_to_list; ?>">Next <?php echo $rows_to_list ?></a> This oughta do it... Regards Joakim Andersson

« previous php.db (#20571) next »