Jump to content

Archived

This topic is now archived and is closed to further replies.

Sam Granger

Want to do a loop, how can I do this?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.