0

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>
3
  • I am trying to fill a list automatically according to images located in a directory. last night I asked here how I can do it and a nice guy came by and sent this script but I didn't understand exactly where I should put the vars part of this script... I believe that the <ul> part goes in my html Commented Mar 8, 2011 at 17:23
  • because I asked it there already and the kind guy answered me and I thought I understood but now when i came to try this script I realized I didn't understand him correctly and I didn't want to bother that guy again with the same question :/ Commented Mar 8, 2011 at 17:27
  • asking in the comments of that answer usually gives you the best and fastest results. Use @ in front of the name of the other person (like I did now) so that he gets a notification of your comment. (don't take it badly, just trying to help you and make your stay at SO more efficient ;) ) Commented Mar 8, 2011 at 17:28

5 Answers 5

2

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.

$type = 'c';

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'.

$imagesDir = '/prettyphoto/images/';

This is where you specify your images directory (where the files are sitting on the server).

$images = glob($imagesDir . $type . '*[TYPE].{jpg,jpeg,png,gif}', GLOB_BRACE);

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

<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>

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

glob("N*T.jpg")

to

glob("/prettyphoto/images/N*T.jpg")
Sign up to request clarification or add additional context in comments.

7 Comments

thank you for your insight. I understood the script but I didn't understand where I need to place it to work. I am new to php.
Hi Jake, not a problem! I updated the answer to describe a little more about where to place it. If you could use an example of a full page that uses this code in context let me know and I would be happy to provide one.
@slifty just placed it in a full html page for test and it only shows this one <ul> bullet... I updated the first post with the source
Can you tell me what files are in the /prettyphoto/images/ directory?
@slifty these are the file names N1F.jpg N1T.jpg N2F.jpg N2T.jpg N3F.jpg N3T.jpg
|
1

Wrap PHP code in php open/close tags "<?php ?>". Also be sure to properly place your open/closing html tags.

<?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); ?>" alt="" /></a></li>
<?php endforeach; ?>

</ul>

Comments

1

You have placed </a> tag inside <img> tag.

<li><a href="<?php echo str_replace('[TYPE]', 'F', $image); ?>"><img src="<?php echo str_replace('[TYPE]', 'T', $image); ?>" alt="" /></a></li>

Comments

0

The problem is with your HTML:

<a href="<?php echo str_replace('[TYPE]', 'F', $image); ?>"><img src="<?php echo str_replace('[TYPE]', 'T', $image); ?></a>" alt="" />

Should be:

<a href="<?php echo str_replace('[TYPE]', 'F', $image); ?>"><img src="<?php echo str_replace('[TYPE]', 'T', $image); ?>" alt="" /></a>

You had your </a> in the wrong spot.

Comments

-3

You also have some basic syntax errors. PHP uses curly braces { ... } to surround the foreach, and there's no endforeach command, you just use a terminating curly brace. The second part should look like this:

<?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>
    </li>
<?php } ?>

4 Comments

-1: This is not the problem, the syntax is correct. Please read: php.net/manual/en/control-structures.alternative-syntax.php
-1: Agreed with @Rocket. Not an error. (personally, I really dislike the endforeach syntax, but it is valid)
@rocket @spudley huh, thanks for pointing that out... I've never seen the alternate syntax.
Yup, PHP's got lots of weird stuff in it. Learn something new everyday.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.