Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Looking for help trying to auto populate ul for image gallery

    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.

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

    PHP Code:
    <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.
    Last edited by mjocon; 07-28-2012 at 06:23 PM.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,426
    Thanks
    17
    Thanked 272 Times in 272 Posts
    Is der a shortage of curly brackets in your neighborhood? You need them to run a loop.
    PHP Code:
    <?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.
    };
    ?>

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by mjocon View Post
    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.

    Code:
    <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:
    PHP Code:
    list($width$height) = getimagesize($imgdir); 
    Quote Originally Posted by DrDOS View Post
    Is der a shortage of curly brackets in your neighborhood? You need them to run a loop.
    OP is using the alternative control structure syntax, which is perfectly valid.
    Last edited by Inigoesdr; 07-28-2012 at 08:47 PM.

  • Users who have thanked Inigoesdr for this post:

    mjocon (07-29-2012)

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,426
    Thanks
    17
    Thanked 272 Times in 272 Posts
    Quote Originally Posted by Inigoesdr View Post
    OP is using the alternative control structure 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.

  • #5
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    So I tried what you said Inigoesdr, here's the code

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

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by DrDOS View Post
    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.

    Quote Originally Posted by mjocon View Post
    So I tried what you said Inigoesdr, here's the code

    PHP 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:
    PHP Code:
    list($width$height) = getimagesize($imgdir $image); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •