I have this PHP code which attempts to populate an HTML ul list.
I’m not sure how to put it together. Here is the PHP:
<?php
$type = 'c';
$imagesDir = '/prettyphoto/images/';
$images = glob($imagesDir . $type . '*[TYPE].{jpg,jpeg,png,gif}', GLOB_BRACE);
?>
<ul>
<?php foreach($images as $image): ?>
<li><a href="<?php echo str_replace('[TYPE]', 'F', $image); ?>"><img src="<?php echo str_replace('[TYPE]', 'T', $image); ?></a>" alt="" /></li>
<?php endforeach; ?>
</ul>
Here is the HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$type = 'N';
$imagesDir = '/prettyphoto/images/thumbnails';
$images = glob($imagesDir . $type . '*[TYPE].{jpg,jpeg,png,gif}', GLOB_BRACE);
?>
<ul>
<?php foreach($images as $image): ?>
<li><a href="<?php echo str_replace('[TYPE]', 'F', $image); ?>"><img src="<?php echo str_replace('[TYPE]', 'T', $image); ?>" alt="" /></a></li>
<?php endforeach; ?>
</ul>
</body>
</html>
Edit: This simplified script works for me:
<ul>
<?php
foreach (glob("N*T.jpg") as $image): ?>
<li>
<a href="<?php echo str_replace("T", "F", $image); ?>">
<img src="<?php echo "$image"; ?>">
</a>
</li>
<?php endforeach; ?>
</ul>
The code as it stands is a little odd, since the first few lines are PHP but they aren’t wrapped in tags. I would also suggest you revise your question to be more specific about what exactly you want to happen.
In the mean time, I’ll explain the code for you so you can better understand what is going on.
This line seems to modify a parameter to your path select. As of now it is indicating that you are ONLY selecting files which begin with the letter ‘c’.
This is where you specify your images directory (where the files are sitting on the server).
The basic idea of this line is that it collects a list of files which fit a particular form. You can learn more about what this function does by looking at the PHP manual for it
This code is pretty direct — it just iterates through the image and displays each image. the foreach method takes a list and runs the code once for each item in the list. The inner stuff is just constructing an HTML image tag, using the information for the current item.
Since PHP is a scripting language, you can either keep this block of code together, or separate it to put the variable definitions on the top of the page, and the HTML portion in the HTML. You just have to maintain the same order.
The key is that the
<ul></ul>stuff is output, and you should place it wherever you want it to appear on your page. The $images code can appear at any point, so long as it appears ABOVE the<ul>code.It looks like your problem is that [TYPE] is not doing what you expect it to do. Right now it is defining a character class, so it selects files with the letter ‘T’ ‘Y’ ‘P’ or ‘E’ in them.
Try removing the [TYPE] clause from the glob string by changing it to:
$images = glob($imagesDir . $type . ‘*.{jpg,jpeg,png,gif}’, GLOB_BRACE);
Then try print_r($images) to see a list of the images just to help you debug.
Make sure you read up on how glob works! It will save you time in debugging.
If you want to find images in a specific path, simply add that path to your glob string.
In other words, if you wanted all images in ‘/prettyphoto/images/’ you would change
to