unistake Posted November 9, 2010 Share Posted November 9, 2010 hi all, I have possibly up to 9 images that I want to find and show on a webpage. I am trying to put a $file in a loop and get all the .jpg images that are found in to an array and show them all. I think from the script you can see what I am attempting to do! Any pointers would be grateful for <?php while($i=0 $i < 9 $i++) { $file = "aircraft/".$_GET['reg'].$i.".jpg"; } foreach($file) { echo "<li class=\"active\"><img src=\"./aircraft/".$reg.$i.".jpg\" alt=\"Flowing Rock\" style=\"width: auto; height: 70px; margin-left: -28.5px; display: block;\" class=\"thumb\" ><br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/ Share on other sites More sharing options...
ninedoors Posted November 9, 2010 Share Posted November 9, 2010 First off you are making $file a variable when I think you want to make it an array. You would need something like: while($i=0 $i < 9 $i++) $file[] = "aircraft/".$_GET['reg'].$i.".jpg"; Then you have a foreach loop. Why not do this inside the while loop? Like this maybe: while($i=0 $i < 9 $i++) echo '<li class="active"><img src="/aircraft/'.$_GET['reg'].$i.'.jpg" alt="Flowing Rock" style="width: auto; height: 70px; margin-left: -28.5px; display: block;" class="thumb" ></li>'; Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132200 Share on other sites More sharing options...
unistake Posted November 9, 2010 Author Share Posted November 9, 2010 Like this? Something is still wrong? <?php while($i=0 $i < 9 $i++) { $file[] = "aircraft/".$_GET['reg'].$i.".jpg"; echo "<li class=\"active\"><img src=\"./aircraft/$file.jpg\" alt=\"Flowing Rock\" style=\"width: auto; height: 70px; margin-left: -28.5px; display: block;\" class=\"thumb\" ><br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132202 Share on other sites More sharing options...
ninedoors Posted November 9, 2010 Share Posted November 9, 2010 Look at my above post. Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132204 Share on other sites More sharing options...
unistake Posted November 9, 2010 Author Share Posted November 9, 2010 I think I am missing something from your post above, I cant find a difference. I put both the $file[] array and echo in the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132206 Share on other sites More sharing options...
Adam Posted November 9, 2010 Share Posted November 9, 2010 Each time you set $file you were overwriting the previous $file, an array would be best. However a foreach() is definitely the preferred way to loop through it, not a while(). Although I'm wondering if you need two loops? Do you loop through the second time later in the code, but just posted them together on here? If not, then why not just echo out from the first loop..? while($i=0 $i < 9 $i++) { echo '<li class="active"><img src="./aircraft/' . $_GET['reg'] . $i . 'jpg" alt="Flowing Rock" style="width: auto; height: 70px; margin-left: -28.5px; display: block;" class="thumb" /></li>'; } Also if you're asking for tips, using single quotes to echo out HTML is much easier to read and write. Plus would it not be better to place the styles in a CSS class? Edit: Missed ninedoors' comment about using 2 loops before. Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132210 Share on other sites More sharing options...
ninedoors Posted November 9, 2010 Share Posted November 9, 2010 I have put everything inside the one loop so there is no reason to collect them in the $file variable. You are also gettign the wrong image reference. Your code will give you <img src="./aircraft/aircraft/'$_GET['reg'].$i.'.jpg.jpg" /> Plus you don't close your li tag, you have a br tag instead. Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132212 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2010 Share Posted November 9, 2010 I recommend getting a list of the matching available files into an array using glob, then iterating over that array. Your existing code will output empty values/php errors for the lines where there are not matching images. Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132214 Share on other sites More sharing options...
unistake Posted November 9, 2010 Author Share Posted November 9, 2010 I am using the script below but I am getting no return, and a white screen. <?php while($i=0 $i < 9 $i++) { echo '<li class="active"><img src="./aircraft/' . $_GET['reg'] . $i . '.jpg" alt="test"><br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132217 Share on other sites More sharing options...
Adam Posted November 9, 2010 Share Posted November 9, 2010 Oh, I copied the loop from a previous post. It's missing the semi-colons between the expressions: <?php while($i=0; $i < 9; $i++) { echo '<li class="active"><img src="./aircraft/' . $_GET['reg'] . $i . '.jpg" alt="test"><br />'; } ?> As PFMaBiSmAd said though, this assumes the values are there. Although I'm not sure if glob is what you're after? You seem to be passing the images via a GET parameter.. Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132220 Share on other sites More sharing options...
ninedoors Posted November 9, 2010 Share Posted November 9, 2010 Can you tell us where the image folder is? You have not closed your li tag replace the <br /> with </li>. Why in your img src do you start with "./", it seems odd to me cause I have never seen that before. Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132225 Share on other sites More sharing options...
unistake Posted November 9, 2010 Author Share Posted November 9, 2010 The value $_GET['reg'] is taken from www.websiteexample.com/index.php?reg=abcd The images are stored in a directory off where the scripts are called 'aircraft' In the script I am trying to show all the images (if there are any) that are stored called abcd1.jpg, abcd2.jpg etc up to abcd9.jpg If anyone has a better way to do it please tell me! I thought a foreach would be best.??? Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132228 Share on other sites More sharing options...
Adam Posted November 9, 2010 Share Posted November 9, 2010 Then PFMaBiSmAd was right. Use glob to loop through the files: foreach (glob('./aircraft/' . $_GET['reg'] . '*.jpg') as $file) { echo '<li class="active"><img src="' . $file . '" alt="test" /></li>'; } Although I'd remove any slashes within the 'reg' parameter, to prevent the user viewing other directories (although they're limited to .jpg files only) on your file system. Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132237 Share on other sites More sharing options...
unistake Posted November 9, 2010 Author Share Posted November 9, 2010 Ok got there, works perfectly. Thanks for the tip on the reg. will do. Quote Link to comment https://forums.phpfreaks.com/topic/218196-whats-wrong-with-this-loop/#findComment-1132241 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.