...

View Full Version : Looking for help trying to auto populate ul for image gallery



mjocon
07-28-2012, 04:06 PM
I want to use php to auto populate an ul with each image name, extension, width, and height. Here's the format that I would like it to output to.



<ul>
<li><img src="path/path/image.img" width="840pt" height="620pt" alt="image" />
</li>
</ul>


This is the best I can do with limited knowledge



<body>
<?php
$imgdir = 'path/path/';
$images = glob($imgdir . '*[TYPE].{jpg,jpeg,png,gif}', GLOB_BRACE);
list($width, $height) = getimagesize($imgdir);
?>

<ul>
<?php foreach($images as $image): ?>
<li>
<img src="<?php echo str_replace('[TYPE]', $image); ?>" width="<?php echo $width;?>" height="<?php echo $height;?>" alt"image" />
</li>
<?php endforeach; ?>
</ul>
</body>



Any help would be awesome, thanks.

DrDOS
07-28-2012, 05:39 PM
Is der a shortage of curly brackets in your neighborhood? You need them to run a loop.

<?php
// Whatever else you need to do here.
foreach($images as $image)
{
// Echo the entire block of html for each list item here, properly escaped.
};
?>

Inigoesdr
07-28-2012, 07:44 PM
I want to use php to auto populate an ul with each image name, extension, width, and height. Here's the format that I would like it to output to.



<ul>
<li><img src="path/path/image.img" width="840pt" height="620pt" alt="image" />
</li>
</ul>


This is the best I can do with limited knowledge

Move this line inside of your foreach loop(inside of <?php tags), and change $imgdir to $image:

list($width, $height) = getimagesize($imgdir);


Is der a shortage of curly brackets in your neighborhood? You need them to run a loop.

OP is using the alternative control structure (http://php.net/control-structures.alternative-syntax) syntax, which is perfectly valid.

DrDOS
07-28-2012, 09:39 PM
OP is using the alternative control structure (http://php.net/control-structures.alternative-syntax) syntax, which is perfectly valid.

Interesting, why do they invent things that they don't seem to really need? Thank you for the info anyway.

mjocon
07-28-2012, 11:39 PM
So I tried what you said Inigoesdr, here's the code



<body>
<?php
$imgdir = 'pics/couplesgroups/';
$images = glob($imgdir . '*[TYPE].{jpg,jpeg,png,gif}', GLOB_BRACE);
?>

<ul>
<?php foreach($images as $image):
list($width, $height) = getimagesize($image);
?>
<li>
<img src="<?php echo str_replace('[TYPE]', $image); ?>" width="<?php echo $width;?>" height="<?php echo $height;?>" alt"image" />
</li>
<?php endforeach; ?>
</ul>
</body>


not sure why its not working, all i get is the bullet for an ul.

Inigoesdr
07-29-2012, 04:22 AM
Interesting, why do they invent things that they don't seem to really need? Thank you for the info anyway.

Because at some point there might be a need, or it might just be simpler to perform an action in an alternate matter. It's the same reason there are multiple options for loops, types, logical operators, etc. :)

The alternate control structure specifically is useful in HTML templates, or other places where it'sa little easier(subjectively) to read everything as markup tags.


So I tried what you said Inigoesdr, here's the code



<body>
<?php
$imgdir = 'pics/couplesgroups/';
$images = glob($imgdir . '*[TYPE].{jpg,jpeg,png,gif}', GLOB_BRACE);
?>

<ul>
<?php foreach($images as $image):
list($width, $height) = getimagesize($image);
?>
<li>
<img src="<?php echo str_replace('[TYPE]', $image); ?>" width="<?php echo $width;?>" height="<?php echo $height;?>" alt"image" />
</li>
<?php endforeach; ?>
</ul>
</body>


not sure why its not working, all i get is the bullet for an ul.

View the source of the page and see what it is outputting. You probably need to prepend the folder to the getimagesize() call too like this:

list($width, $height) = getimagesize($imgdir . $image);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum