Jump to content

Semi-automated image gallery


Fmdpa

Recommended Posts

PHP is so cool! But I need help do a couple things. I want to create an semi-automated image gallery. I came out with this code so far.

<?php$images = array('image1.gif','image2.gif','image3.gif','image4.gif','image5.gif','image6.gif');$i = 1;print '<table>';foreach ($images as $thumb)	{$link = ???	/* I want to take the value of the $thumb variable, e.g. image1.gif, remove the file prefix, and replace it with [b]php[/b] (this will link to the image\'s page, which has the same filename as the image, but always has a php file extension) */ print "<tr><td><a href=$link><img src=$thumb class='thumb' /></a></tr>";   /*this sort of does what I want it to do, but what this is what I really want: I want there to be a new table row (<tr>), three separate images (<td></td><td></td><td></td>), then a </tr><tr>, but if there are no more images left in the array ($i == count($images)), then end the row even if the number of images in that row is < 3. */$i++; //if you use the method mentioned in the comment…}?>

Link to comment
Share on other sites

Thanks! That worked for the part I wanted, but I forgot that the thumbnail is in the "thumb/" folder, so how do I trim that out (i.e. thumb/image1.gif -> image1.php)?

Link to comment
Share on other sites

I was thinking about the new row code, and I tried this:

<?php$images = array('image1.gif','image2.gif','image3.gif','image4.gif','image5.gif','image6.gif');$i = 0;print '<table>';foreach ($images as $thumb)	{$i++;print "<tr><td><a href=$link><img src=$thumb class='thumb' /></a>";   	if ($i % 3 == false)	{	 print '</tr>';	 print '<tr>';	}}?>

But it doesn't work.

Link to comment
Share on other sites

"doesn't work" doesn't help us help you. A more detailed description would help more.I can say this:You'd do better to test the result of the % operator against a number (like 0) than a Boolean false. Since it's the simple comparison operator (== instead of ===), it probably works, but it looks weird. :)src=$thumb will not work. Try src='$thumb'

Link to comment
Share on other sites

"doesn't work" doesn't help us help you. A more detailed description would help more.I can say this:You'd do better to test the result of the % operator against a number (like 0) than a Boolean false. Since it's the simple comparison operator (== instead of ===), it probably works, but it looks weird. :)src=$thumb will not work. Try src='$thumb'
To clarify "doesn't work"...the code should add a row end </tr> and begin a new row <tr> every three images. But the images still stay in one column (adds a new row for each image). I didn't try testing the expression against a number yet (as opposed to a Boolean). What does "===" mean?P.S. Thanks for the link, wirehopper. I'll take a look at that download later.
Link to comment
Share on other sites

What does "===" mean?
http://php.net/manual/en/language.operators.comparison.phpwould be said as "exactly equal to", and doesn't type cast. Essentially it will not only test to see if the values are equal, but will ensure that the comparison is being evaluated between similar data types. Is considered to be a stricter form of comparison. I like using it.
Link to comment
Share on other sites

Here is my final code:

<?php$path = thumb;$images = array("$path/image.gif", "$path/image1.gif", "$path/image2.gif", "$path/image3.gif", "$path/image4.gif", "$path/image5.gif", "$path/image6.gif", "$path/image7.gif");$i = 0;print '<table cellPadding="4" cellSpacing="0" summary="image table" id="thumb_table">';foreach ($images as $thumb) {	$i++;	$pat = '/^thumb\/(.*)\.gif$/';	$rep = '$1.php';	$link = preg_replace($pat, $rep, $thumb);	if ($i === 1) {		print '<tr>';	}	print "<td width='10%' align='center' valign='top'><a href='$link'><img src='$thumb' class='thumb'  alt='Image Thumb' /></a></td>";	if ($i % 8 === 0) {		print '</tr><tr>';	}		}print '</table>';?>

It is so easy to use that it seems like cheating. :) It saved me literally hundreds upon hundreds of lines. Adding images is now a breeze. Thanks so much for all your help!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...