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
Showing 10 records per page
Moderator: General Moderators
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
In general the way this is done is by passing a limiter around in a query string. So:
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:
Hope that helps...
Code: Select all
SELECT * FROM blah WHERE foo = 'stinkpot'Code: Select all
$sql = "SELECT * FROM blah WHERE foo = 'stinkpot' LIMIT {$_GET['recordNum'}, 20I believe there is a pagination class in the code snippets forum.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
//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";
}
$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";
}