Sam Granger Posted October 7, 2006 Share Posted October 7, 2006 I have the following sourcecode:<?php$ad = $_GET['a'];$filename = "./uploads/$ad.jpg";if (file_exists($filename)) { echo "<img src='$filename' alt='' />";}?>Currently when i go to filename.php?a=1 it displays 1.jpg if it exists. However, I want it to check if 1_1.jpg, 1_2.jpg, 1_3.jpg etc... exists. Lets say those 3 files exists but 1_4 doesnt, then it has to stop checking (stop the looping). How can I do this? Link to comment https://forums.phpfreaks.com/topic/23257-want-to-do-a-loop-how-can-i-do-this/ Share on other sites More sharing options...
Orio Posted October 7, 2006 Share Posted October 7, 2006 [code]<?php$ad = $_GET['a'];$i=1;$continue=TRUE;while($continue){ $filename = "./uploads/".$ad."_".$i.".jpg"; if (file_exists($filename)) { echo "<img src='$filename' alt='' />"; } else { $continue=FALSE; }$i++;}?>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/23257-want-to-do-a-loop-how-can-i-do-this/#findComment-105429 Share on other sites More sharing options...
Sam Granger Posted October 7, 2006 Author Share Posted October 7, 2006 Thanks! Works like a dream! :) Link to comment https://forums.phpfreaks.com/topic/23257-want-to-do-a-loop-how-can-i-do-this/#findComment-105431 Share on other sites More sharing options...
alpine Posted October 7, 2006 Share Posted October 7, 2006 Or use foreach[code]<?php$files = array("one.jpg","two.jpg","whatever.jpg");foreach($files as $file){ if(file_exists($file)) echo "$file does exist <br />"; else break;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/23257-want-to-do-a-loop-how-can-i-do-this/#findComment-105432 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.