Page 1 of 1
Showing 10 records per page
Posted: Fri Jun 02, 2006 9:50 pm
by aceconcepts
Hi,
I have programmed a search facility that uses SQL to to retrieve specific records from a MYSQL database.
At the moment all retrieved records are displayed in the browser.
I would like to be able to display search results as 10 results per page e.g. like search engines such as Google and also to allow the user to click next to view the next 10 records.
How can I do this?
Thanks.
Nick
Posted: Fri Jun 02, 2006 10:10 pm
by neophyte
In general the way this is done is by passing a limiter around in a query string. So:
Code: Select all
SELECT * FROM blah WHERE foo = 'stinkpot'
If you knew on the previous page that the first 10 records were displayed on the second page you could simple use the same query and slice the result array on the 10 record. Or simply Limit the query:
Code: Select all
$sql = "SELECT * FROM blah WHERE foo = 'stinkpot' LIMIT {$_GET['recordNum'}, 20
Hope that helps...
Posted: Fri Jun 02, 2006 10:24 pm
by s.dot
I believe there is a pagination class in the code snippets forum.
Posted: Sat Jun 03, 2006 3:07 am
by phpmash
//This code select therequires rows from the table
$rows_per_page=10;
if(!isset($screen)){
$screen = 0;
}
$start = $screen * $rows_per_page;
$sql = "SELECT * FROM TABLE where <condition>;
$sql .= "LIMIT $start, $rows_per_page";
$result = mysql_query($sql, );
$rows = mysql_num_rows($result);
//Show the results here
//This code will be at bottom side to represent page numbers
if ($screen > 0) {
$pgn=$screen - 1;
$url = "results.php?screen=" . $pgn . "&pages=" . $pages;
echo "<a href=\"$url\"><font color=#0066FF size=2><u>Previous</u></font></a>\n";
}
// page numbering links now
for ($i = 0; $i < $pages; $i++) {
$url = "results.php?screen=" . $i . "&pages=" . $pages;
$p=$i+1;
echo " | <a href=\"$url\"><font color=#0066FF size=2><u>$p</u></font></a> | ";
}
if ($screen < $pages-1) {
$pgn=$screen + 1;
$url = "results.php?screen=" . $pgn . "&pages=" . $pages;
echo "<a href=\"$url\"><font color=#0066FF size=2><u>Next</u></font></a>\n";
}