webdeveloper123 Posted August 6, 2022 Share Posted August 6, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/ Share on other sites More sharing options...
Solution ginerjm Posted August 6, 2022 Solution Share Posted August 6, 2022 (edited) 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 August 6, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599079 Share on other sites More sharing options...
mac_gyver Posted August 6, 2022 Share Posted August 6, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599080 Share on other sites More sharing options...
webdeveloper123 Posted August 6, 2022 Author Share Posted August 6, 2022 thanks ginerjm i'll give that a go Sorry mac_gyver I don't know what your getting at. I read about the format specifier on php.net are you saying the %s is clashing with width:100%? Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599081 Share on other sites More sharing options...
webdeveloper123 Posted August 6, 2022 Author Share Posted August 6, 2022 12 minutes ago, ginerjm said: Not sure what $filename actually contains it's passed to basename in my original code Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599082 Share on other sites More sharing options...
ginerjm Posted August 6, 2022 Share Posted August 6, 2022 I did a test of my own and my code works just fine with no need to modify the use of $filename. Try it. Don't know why you thought you had to play with the glob output. Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599084 Share on other sites More sharing options...
webdeveloper123 Posted August 6, 2022 Author Share Posted August 6, 2022 8 minutes ago, ginerjm said: I did a test of my own and my code works just fine haha clever guy. Thanks, and thanks for the numbertext, I wasn't sure how to do that one! Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599085 Share on other sites More sharing options...
ginerjm Posted August 6, 2022 Share Posted August 6, 2022 Can I see your final code? Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599086 Share on other sites More sharing options...
Barand Posted August 6, 2022 Share Posted August 6, 2022 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)); Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599087 Share on other sites More sharing options...
webdeveloper123 Posted August 6, 2022 Author Share Posted August 6, 2022 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> "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599088 Share on other sites More sharing options...
webdeveloper123 Posted August 6, 2022 Author Share Posted August 6, 2022 3 minutes ago, Barand said: He is saying that to output a "%" in a format string you need to have "%%". ahh ok. Thanks barand Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599089 Share on other sites More sharing options...
ginerjm Posted August 6, 2022 Share Posted August 6, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599090 Share on other sites More sharing options...
webdeveloper123 Posted August 6, 2022 Author Share Posted August 6, 2022 2 minutes ago, ginerjm said: You need to learn that you DO NOT NEED TO LEAVE PHP MODE just to output a line or two of html. yes you mentioned a few times to me before Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599091 Share on other sites More sharing options...
ginerjm Posted August 6, 2022 Share Posted August 6, 2022 So why do you continue? Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599092 Share on other sites More sharing options...
webdeveloper123 Posted August 6, 2022 Author Share Posted August 6, 2022 I will, it's just the learning process. everyone learns at their own pace, and some people get stuck in bad habits Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599093 Share on other sites More sharing options...
ginerjm Posted August 6, 2022 Share Posted August 6, 2022 Or is it because you aren't learning but rather continuing to copy other people's work as your own? How hard can it be to catch on to not leaving php mode? Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599094 Share on other sites More sharing options...
Barand Posted August 6, 2022 Share Posted August 6, 2022 6 minutes ago, ginerjm said: So why do you continue? Perhaps he hasn't realized yet that ignoring your commandments can cause your wrath to descend on him in the form of seven deadly plagues. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599095 Share on other sites More sharing options...
webdeveloper123 Posted August 6, 2022 Author Share Posted August 6, 2022 Who's work did I copy as my own? I marked as your solution, I never claimed I wrote that code. That's how a forum works. You get stuck, ask a a question and sometimes you get the correct answer. Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599096 Share on other sites More sharing options...
ginerjm Posted August 6, 2022 Share Posted August 6, 2022 And that is not at all what I was referring to. Never mind. Just wasting my time here. Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599097 Share on other sites More sharing options...
webdeveloper123 Posted August 6, 2022 Author Share Posted August 6, 2022 2 minutes ago, Barand said: Perhaps he hasn't realized yet that ignoring your commandments can cause your wrath to descend on him in the form of seven deadly plagues. loooooooool Quote Link to comment https://forums.phpfreaks.com/topic/315148-some-help-using-glob-and-foreach-loop/#findComment-1599098 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.