What is the best way to have a list from a MySQL database come up only 10 at a time?

Glenn

Member
I need to have a user list something from a database that shows up 10 at a time and they can select to see the next 10 and so on. What is the best way to accomplish this?

Would also like to have a drop down form for if they want more per page.
 

ccowan

New Member
What you want to do is add a limit to the query. For example:

$result = mysql_query("SELECT * FROM `tablename` WHERE `field` = '$variable' ORDER BY `field` ASC LIMIT $offset, $rowsPerPage");
while ($r = mysql_fetch_array($result)) {
//do your thing here
}

If the $offset was 10 the list will start at the 10th result it finds. The $rowsPerPage is how many result it will list. So, you can have the $rowsPerPage variable actually get set by a SESSION which is set from the drop down you want. Then have the $offset set to whatever page they are on. So if they just came to the page the offset will be 0. Then you can have some pagination at the bottom that lets them select pages. If they select page 2, it will send them back to the same URL but with a GET variable for the $offset set as 10.

This is obviously not a full tutorial but should get you started.

Hope this helps!
 
Top