Hey all, I recently put together this photo album from a lesson I read in a PHP Beginner book. It works great, but the book did not cover how to organize the uploaded images into a designated number of columns and rows, as well as the addition of pagination.

Would someone be able to provide me with some direction as to how I add these important features to this piece of code that I have. It would be a great learning experience for me.

<?php
//connect to the database
$link = mysql_connect("localhost", "myName", "Password")
or die("Could not connect: " . msql_error());
mysql_select_db("myName", $link)
or die (mysql_error());
$ImageDir = "images/";
$ImageThumb = $ImageDir . "/thumbs/";
?>
<html>
<head>
<title>Welcome to our Photo Gallery</title>
</head>
<body>
<div align="center">
<center>
<table border="0" cellpadding="2" cellspacing="1" width="80" bgcolor="#000000">
<?php
//If cmd is not hit
if(!isset($cmd))
{
//get the thumbs
$getpic = mysql_query("SELECT * FROM profileimages")
or die(mysql_error());
while ($rows = mysql_fetch_array($getpic)) {
$id=$rows["image_id"];//take out the id
extract($rows);
echo "<tr>\n";
echo "<td bgcolor='#CCCCCC'><p align='center'>\n";
echo "<a href=\"".$ImageDir . $image_id . ".jpg\">\n";
echo "<img src=\"" . $ImageThumb . $image_id . ".jpg\" border=\"0\">";
echo "</a>\n";
echo "</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td bgcolor='#EFEFEF'><p align='center'>" . $image_caption . "</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td bgcolor='#FFFFFF'><p align='center'>\n";
echo "<a href='phgallery.php?cmd=delete&id=$id'>Delete</a>\n";
echo "</td>\n";
echo "</tr>\n";
}
}
if($cmd=="delete")
{
    $sql = "DELETE FROM profileimages WHERE image_id=$id";
    $getpic = mysql_query($sql);
    echo "Image deleted!<br>";
    echo "<a href='phgallery.php'>Return to the Gallery</a>\n";
}
?>
</table>
</center>
</div>
</body>
</html>

I'd like to set it to display the images in 4 column 4 rows.

An example can be found here:
http://www.toofly.com/phgallery.php

I've searched many areas but couldn't find any decent advice that fits this particular project.

    For columns:
    Use an incementer variable ($i) inside your mysql_fetch_array() loop. If $i reaches 4, start a new table row (tr) and unset($i), otherwise start a new table column (td). There's a little more to it then that, but it should get you started.

    For basic paging:

    $images_per_page = 16;
    $start_offset = $images_per_page * $_GET["page"];
    
    $getpic = mysql_query("SELECT * FROM profileimages LIMIT $start_offset, $images_per_page") 
    or die(mysql_error()); 
    while ($rows = mysql_fetch_array($getpic)) { 
        //show the thumbs
    }
    
    //later we display the next / previous links (or 1,2,3,4) links like this one
    echo "<a href='?page=" . $_GET["page"]+1) . "'>Next</a>";
    

    That's as basic as i could make it, there are minor issues with it you will notice probably, but its just to get you started. Good luck.

      Thanks, I'll give that a shot, and report back on my progress.

        5 days later

        I'm still having problems with this.

        I had no idea it would be this hard to extract my images into....

        4 column 4 rows.

        on each page.

          here's a down an dirty gallery I threw together, feel free to chop it up and modify it. It's not pretty or efficient because I'm still learning but maybe you can find what you need in it. hope this helps.

          <?php
          /* Cycle through the images and display them */
          $count = "0";
          $imghtml = "<img align=\"center\" width=\"75%\" border=\"0\" src=\"";
          $images  = array ("sw11", "sw12", "sw13", "sw14", "sw15", "sw16", "sw17",
              "sw18", "sw19", "sw20");
               foreach ( $images as $img) {
               if ($count %= "3") {
               print "<td align=\"center\">";
               print "<a href=\"$img.jpg\">";
               print "$imghtml/imagestek/spacewalk/$img.jpg\"></a></td>";
               } else {
               print "<tr><td align=\"center\">";
               print "<a href=\"$img.jpg\">";
               print "$imghtml/imagestek/spacewalk/$img.jpg\"></a></td>";
               };
               $count++;
              }
          print "<td><br /></td>";
          print "<td><br /></td>";
          print "<tr><th colspan=\"3\"><a href=\"index.php\">Previous 10</a></th>";     
          print "<tr><th colspan=\"3\"><a href=\"swindex1.php\">Next 10</a></th>"; ?>

          BTW i saw a gallery project on the front page of this very site, so if this doesn't help, you maybe you should seek out that project.

            Write a Reply...