Showing 10 records per page

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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Showing 10 records per page

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I 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.
User avatar
phpmash
Forum Newbie
Posts: 24
Joined: Mon Oct 31, 2005 6:41 am
Location: Kerala,India
Contact:

Post 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";
}
Post Reply