On my website I have Sockigami models, and for [URL=http://sockigami.com/models/category/entertainment]this[/URL] page (displaying all of the models), I would like to make the user click “next page” for every 10 models or so (it’s not done yet, the list will have more information in it). Can someone give me some example code for how to do this?
@TaschenSep 22.2006 — #Yep! It's called pagination.
First: decide how many results you want per page and asign a $limit with the value
Then: using the count() function to count the total number of records limit the results using the $limit variable.
Finally: print results to page using the count() and $limit values to move forwards and backwards.
[code=php] $limit = 10; $start = $_GET['start']; // one way or another get a start position //query the db to get total records $query = "SELECT COUNT(*) FROM table WHERE published = 1 [etc]"; $result = mysql_query($query); $row = mysql_fetch_row($result); $total = $row[0]; //Don't forget some error control and flush results around here
//New query to get the actual records $query = "SELECT something FROM somewher WHERE condition LIMIT $start, $limit";
//execute query etc ($result = mysql_query($query);) and iterate over your //result set (if any!) [/code] Next you will need a forward and back link. No problem. [code=php] if($start >= $limit){ echo "<a href="mypage.php?start=".($start-$limit."">previous</a>"; } //next link if($start + $limit < $total && $start >= 0 ){ echo "<a href="mypage.php?start=".($start+$limit."">next</a>"; } [/code]