Mega Code Archive

 
Categories / Php / MySQL Database
 

This shows how to page thru a resultset in a simple manner

It produces a set of links where-ever its called that looks like: [ 1 2 3 4 5 6 7 ] and so on. For extremely large resultsets, this function may not be aproppriate as the list of links could get very long, though you can mitigate that by using a larger display amount value, which is the number of items to show on any one page. <? /* show_pages shows the links to the different pages it gets passed the current offset and the total number of images to show on one page ($display_length) */ function show_pages($offset, $display_length) { //run a query to see how many records there are total. $total = ''; $total_images = 0; $pages = 0; $base_offset = 0; echo "<table width='100%'><tr><td align='center'><b>[ "; $sql = "select count(*) from photos"; //query is a custom db connect function $total = query($sql); if (mysql_num_rows($total)==1) { $row = mysql_fetch_array($total); $total_images = $row[0]; $pages = ceil($total_images/$display_length); //calc the number of links to show (ceil rounds up to next int) for ($x = 1 ; $x <= $pages; $x++) { //don't show current page as link if ($base_offset == $offset) { echo "$x "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?offset=$base_offset\">$x</a> "; }//end if $base_offset += $display_length; }//next }else{ echo "No other images found"; } echo "]</b></td></tr></table>"; } ?>