Page 1 of 1

Adding descriptions to Image Gallery?

Posted: Fri Jan 08, 2010 4:01 am
by offthebeatentrack
Hi everyone, first post!

I have started working on an image gallery for a website I am creating. The basic code I am using below works without any issues, but I would like to be able to add the following functionality

Thumbnails would have a short description below each one.
When a thumbnail is clicked, its associated full size image will appear in a Javascript-written pop-up containing the short description and also a long description.

I would like the descriptions to be stored in a flat file, without using MySQL as the server I am using does not allow MySQL.

This is the the code I have used to create my image gallery.

Code: Select all

<?php
$a = '0';
$filepath = "thumb";
$url_path = "main";
$dir = dir($filepath);
echo "<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\" width=\"75%\">";
while($entry=$dir->read()) {
    if($entry == "." || $entry == "..") { 
        continue; 
    } 
    $fp = @fopen("$filepath/$entry","r");
if ($a == '0') {echo "<tr>";}
if ($a == '5') {echo "<tr>";}
if ($a == '10') {echo "<tr>";}
if ($a == '15') {echo "<tr>";}
?><td>
  <a href="<? echo "$url_path/$entry" ?>">
  <img src="<? echo "$filepath/$entry" ?>" alt="<? echo $entry ?>"></a>
  </td>
<?
$a = $a + 1;
}
?> 
 
What would I need to add to this to allow the use of short and long descriptions?

Thank you in advance to anyone who can help me with this. :D

Re: Adding descriptions to Image Gallery?

Posted: Fri Jan 08, 2010 2:18 pm
by manohoo
For starters replace your entire WHILE statement with this, easier to read.

Code: Select all

while($entry=$dir->read()) {
    if($entry == "." || $entry == "..") {
        continue;
    }
    $fp = @fopen("$filepath/$entry","r");
if ($a == '0') {echo "<tr>";}
if ($a == '5') {echo "<tr>";}
if ($a == '10') {echo "<tr>";}
if ($a == '15') {echo "<tr>";}
echo "<td>";
echo "<a href='$url_path/$entry'";
echo "<img src='$filepath/$entry'alt='$entry'></a>";
echo "</td>";
$a = $a + 1;
}

Re: Adding descriptions to Image Gallery?

Posted: Fri Jan 08, 2010 2:37 pm
by manohoo
If you can put filename, short description, long description into an array then it should be easy.
Say:

$array = array();
$array[0]=('image1.jpg','dog','my dog taking a nap');
etc...

Your while loop can look something like this:

Code: Select all

 
while($entry=$dir->read()) {
    if($entry == "." || $entry == "..") {
        continue;
    }
    $fp = @fopen("$filepath/$entry","r");
if ($a % $ipr == '0') {echo "<tr>";}
echo "<td>";
echo "<a href='$url_path/$entry'";
echo "<img src='$filepath/$entry'alt='$entry'></a>";
echo "<br />$array[$a][1]"; //short description
echo "<br />$array[$a][2]"; //long description 
echo "</td>";
$a++;
} 
 

Re: Adding descriptions to Image Gallery?

Posted: Fri Jan 08, 2010 3:06 pm
by offthebeatentrack
Thanks for the reply.

I've changed the code like you said, but it keeps throwing up a division by zero apparently because of this:
if ($a % $ipr == '0') {echo "<tr>";}
Also when I try to add the array I get this:
Parse error: syntax error, unexpected ',' in C:\Users\Admin\Desktop\newTest\index.php on line 13

Re: Adding descriptions to Image Gallery?

Posted: Fri Jan 08, 2010 3:44 pm
by manohoo
Study this code:

Code: Select all

echo "<table border='1'>"; 
 
$ipr = 5; // cells per row
$a=0; // counter
for ($a=0;$a < 20; $a++) {
  if ($a % $ipr == '0') {
    echo "<tr> <td> $a </td>";} 
  else {
    echo "<td> $a </td>";}
  }
echo "</table>";
% is a modulus operator, it will return the remainder of a division. For instance:

Code: Select all

echo 10%3; // would return 1