I'm trying to develop a web gallery, but have been struggling with laying out the thumbnails, and paginating the results. I found a tutorial, but have been experiencing problems with it. Can anyone point me in the right direction?
Current result: http://www.equinefocus.co.uk//photos/2009-03-21/gallery.php
Current code:
<?php
// Connects to your Database
mysql_connect("*****", "*****", "*****") or die(mysql_error());
mysql_select_db("*****")
or die ("Unable to connect to MySQL");
// How many pictures to display on each page.
$picsperpage = 12;
// How many pictures to display per row.
$picsperrow = 4;
// images
$thumb ='/photos/'.$info['date'].'/thumbs/'.$info['thumb'];
$url ='/photos/'.$info['date'].'/'.$info['url'];
// If the page number is set...
if(isset($_GET['p'])) {
// $page is now the page number.
$page = $_GET['p'];
} else {
// Otherwise the page is 1.
$page = 1;
}
// What the starting number will be for the query.
$start = ($page - 1) * $picsperpage;
// Counts all of the rows in the table.
$res= @mysql_query("SELECT COUNT(id) AS numrows FROM photos")
or die("Sorry, but there was an error retrieving the gallery.");
// Gets the number of rows from the array.
$fetchres = @mysql_fetch_array($res, MYSQL_ASSOC) or die("Sorry, but there was an error retrieving the
gallery.");
$numrows = $fetchres['numrows'];
// How many pages there will be (total pictures / # of pictures per page).
$totalpages = ceil($numrows/$picsperpage);
echo '<ul>';
// Echos our page numbers so that we can navigate.
for($i=1; $i <= $totalpages; $i++) {
if($i == $page) {
// If it's the current page, don't make a link since we are already there.
echo '<li>' . $i . '</i>';
} else {
// Make all other pages links.
echo '<li><a href="main.php?p=' . $i . '">' . $i . '</a></li>';
}
}
echo '</ul><br />
<div class="gallery">';
// Get all of the pictures for the current page.
$allphotos = @mysql_query("SELECT * FROM photos LIMIT $start, $picsperpage")
or die("Sorry, but there was an error retrieving the gallery.");
// How many pictures there are right now.
$rowcount = 0;
// For each row in the gallery table...
while ($row = @mysql_fetch_array($allphotos)) {
// If $rowcount divided by $picsperrow has a remainder of 0...
if (($rowcount % $picsperrow) == 0) {
// Make a <br /> tag with a clear both style, so that there is a new row of images
echo '<br style="clear: both;" />';
}
// Displaying each image with this code.
echo '<a href="' . $row['thumb']. '"><img src="' . $row['url']. '" /></a>';
// $rowcount + 1.
$rowcount++;
}
// After all the images are done, end the div.
echo '</div>';
?>
Attached is a screenshot of my current database table.