Dividing lots of content into multiple pages using PHP/MySQL

I’ve got close to 30 or so photos on my site, and I’d like them to be displayed in sets of 10, with a “Previous” and “Next” link allowing progress backward or forward through the pictures. You’ve all seen it, but can you help me to do it?

Click here for a text version of what I’ve got

http://img217.imageshack.us/img217/9322/untitled1vr4.gif

Any help is greatly appreciated.

Well it would help if you told us what yer problem is :wink:

BTW shouldn’t:
for ($id=$start;$id++;$id<=$end)
actually be:
for ($id=$start; $id<=$end; $id++)

whoa, can’t help you there

PS - I like Python a LOT!

hmmm, you’re absolutely right. How did I miss that!

You are hardcoding your $start and $end values.

$start is always 1
$end is always 10

               $_SESSION['end'] = "10"; 
               $start = ($_SESSION['end'] - 9); 
               $end = $_SESSION['end']; 

You’re looping over an SQL select … why?

You create a loop and select one picture each time. That’s an incredibly inefficent way to connect with your DB. You should let the database do the work for you and select all the rows you want at once.

SELECT (the_stuff_you_want) FROM (table) WHERE (field) BETWEEN (start) AND (end)

You’ll need to substitute the placeholders with the right ones for your situation.

You then loop over your returned result set for the output, but to initiate a DB transaction each time is wasteful.

You probably want to start with a ‘select count …’ to find out how many records you’re dealing with total. Divide by the number you want to display per page (to figure out how many pages you need) and then assign a ‘page number’ as a local page variable. You can calculate start and end by doing (page_number * number_per_page) and ((page_number+1) * number_per_page)

That way you can just have links on the page to ‘?page=2’ etc.
You know never to display a link for pages <0 or more than the total you calculated.
You may also want to adjust the numbers so that you don’t have ‘page=0’ but that’s a personal preference. If you want the first page to be ‘page=1’ you’ll need to adjust your calculations accordingly.

The only gotcha with that is if your DB changes content between page views (as you may miss a picture).

Thanks for the replies everyone… I’ve revised the code heavily, based on your suggestions and some from a friend of mine.
I’m happy to report that the code is now completely functional.


            # -------------------------------------------------
            #   photography page content
            # -------------------------------------------------
            case 'photography':
               global $db;
               
               if (isset($_GET['start'])) { // Check if we've already got a row to start from
                  $numPhotos = 4;
                  $start = (($_GET['start'] * $numPhotos) + 1);
                  $end = (($_GET['start'] * $numPhotos) + 4);
               } else { // Default to row zero
                  $numPhotos = 4;
                  $start = 0;
                  $end = 4;
               }
               
               connect($error, $type); 
               $result = mysql_query("SELECT * FROM art_photo WHERE id BETWEEN " . $start . " AND " . $end) or die(mysql_error()); 
               $photo = mysql_fetch_assoc($result); 
               
               $open = '&lt;a href="#" onClick="MyWindow=window.open(\'';
               $mid = "','MyWindow','scrollbars=yes,";
               $left = ($photo['width']/7);
               $top = ($photo['height']/2);
               $close = "'); return false;\"&gt;";
               $img = '&lt;img class="img" border="0" src="';
               
               print '&lt;h1&gt;my photography (&lt;a href="http://hobbes.cjb.cc/art.php?page=photography"&gt;standard&lt;/a&gt; | &lt;a href="http://hobbes.cjb.cc/art.php?page=panoramic"&gt;panoramic&lt;/a&gt;)&lt;/h1&gt;&lt;br/&gt;' . "
";
               print '&lt;table cellpadding="0" cellspacing="0" width="100%"&gt;' . "
";
               print '   &lt;tr&gt;' . "
";
               print '      &lt;td colspan="2"&gt;' . "
";
               print 'Start: ' .$start;
               print 'End:' . $end;
               print '      &lt;/td&gt;' . "
";
               print '   &lt;/tr&gt;' . "
";
               do {
                 print '   &lt;tr&gt;' . "
" .
                         '      &lt;td height="125" width="160"&gt;' . "
";
                 print           $open . $photo["image"] . $mid . 'width=' . $photo["width"] . ',height=' . $photo["height"] . ',left=' . $left . ',top=' . $top . $close . $img . $photo["thumb"] . '" alt="' . $photo["title"] . '" title="' . $photo["title"] . '"/&gt;&lt;/a&gt;&lt;/td&gt;' . "
" .
                         '      &lt;td style="padding-top: 10px;"&gt;' . "
" .
                                   $photo['desc'] . "
" .
                         '      &lt;/td&gt;' . "
" .
                         '   &lt;/tr&gt;' . "
";
               } while ($photo = mysql_fetch_assoc($result));
               
               $query = mysql_query("SELECT * FROM art_photo"); 
               $totalRows = mysql_num_rows($query); // Get the total number of rows we have
               $totalPages = ceil($totalRows/$numPhotos)-1;  // Divide the number of rows by the number of images we want per page
               
               print '   &lt;tr&gt;' . "
" .
                       '      &lt;td colspan="2" style="text-align: center"&gt;' . "
";
               print '      &lt;a href="http://hobbes.cjb.cc/art.php?page=photography&start=0"&gt;First&lt;/a&gt; | ';
               print '      &lt;a href="http://hobbes.cjb.cc/art.php?page=photography&start=' . ($_GET['start'] - 1) .' "&gt;Previous&lt;/a&gt; | ';
               print '      &lt;a href="http://hobbes.cjb.cc/art.php?page=photography&start=' . ($_GET['start'] + 1) .' "&gt;Next&lt;/a&gt; | ';
               print '      &lt;a href="http://hobbes.cjb.cc/art.php?page=photography&start=' . $totalPages . ' "&gt;Last&lt;/a&gt;';
               print '      &lt;/td&gt;' . "
" .
                       '   &lt;/tr&gt;' . "
" .
                       '&lt;/table&gt;';
               
               mysql_free_result($result); 
               break;

The page: http://hobbes.cjb.cc/art.php?page=photography