Jump to content

some help using glob and foreach loop


Go to solution Solved by ginerjm,

Recommended Posts

Hi Guys,

I am creating an image gallery. I already have working code to select and upload a image into a directory. I have successfully used glob and a foreach loop to display the images from that folder on to a page. I found some code on "W3C How to" which had ready made image gallery with css and JS included. Here is the link: https://www.w3schools.com/howto/howto_js_slideshow.asp

What I am trying to do is echo the html in a foreach loop. I am doing ok so far but I can't get an html attribute into my echo code. Here is what I am trying to echo:

<div class="mySlides fade">
    <div class="numbertext">1 / 3</div>
    <img src="img1.jpg" style="width:100%">  
  </div>

Here is what I have:

$files = glob("images/*.{jpg,png,gif,svg,jpeg,bmp,webp}", GLOB_BRACE);


foreach ($files as $filename) {
           echo '<div class="mySlides fade">';
           echo '<div class="numbertext">1 / 3</div>';
        printf("<img src='images/%s'/>" , basename($filename));

}

The above code does display all images from the folder "images" onto the page so that's fine.

Now the printf replaces the 

           <img src="img1.jpg" style="width:100%">;

But I can't seem to get the 

style="width:100%"

Into the printf statement. I have been trying for hours, escaping characters, going in and out php etc etc but Can't figure it out

Can someone help please?

Link to comment
https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/
Share on other sites

  • Solution

Don't know why the printf is needed.  Here is how I would tackle it, no testing of course.  I'll leave that to you.

$files = glob("images/*.{jpg,png,gif,svg,jpeg,bmp,webp}", GLOB_BRACE);
$cnt = count($files);
$i = 0;
foreach ($files as $filename) 
{
	$i++;
	echo "
		<div class='mySlides fade'>
		<div class='numbertext'>$i / $cnt</div>
		<img src='$filename' style='width:100%;'>
		</div>
		";
}

Not sure what $filename actually contains but you can easily append anything needed to the src= attribute clause if it's not already there

Edited by ginerjm

the format specifier character is a % and the format specifier for a literal % is %, so the result is %%. this is similar to the %s you are also using. the % is the format specifier character. the s is the format specifier for a string argument, resulting in %s.

17 minutes ago, webdeveloper123 said:

are you saying the %s is clashing with width:100%?

He is saying that to output a "%" in a format string you need to have "%%".

printf("<img src='images/%s' style='width: 100%%' />" , basename($filename));

 

3 minutes ago, ginerjm said:

Can I see your final code?

<?php


$files = glob("images/*.{jpg,png,gif,svg,jpeg,bmp,webp}", GLOB_BRACE);
$cnt = count($files);
$i = 0;

?>

<div class="slideshow-container">

  <?php
foreach ($files as $filename) 
{
  $i++;
  echo "
    <div class='mySlides fade'>
    <div class='numbertext'>$i / $cnt</div>
    <img src='$filename' style='width:20%;'>
    </div>
    ";
}


?>

 

So close.  You need to learn that you DO NOT NEED TO LEAVE PHP MODE just to output a line or two of html.

$files = glob("images/*.{jpg,png,gif,svg,jpeg,bmp,webp}", GLOB_BRACE);
$cnt = count($files);
$i = 0;
echo "<div class='slideshow-container'>";
foreach ($files as $filename) 
{
  $i++;
  echo "
    <div class='mySlides fade'>
    <div class='numbertext'>$i / $cnt</div>
    <img src='$filename' style='width:20%;'>
    </div>
    ";
}

Entirely in php mode!

HTH

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.