Jump to content


Photo

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


  • Please log in to reply
3 replies to this topic

#1 Sam Granger

Sam Granger
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 07 October 2006 - 08:33 AM

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?

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 07 October 2006 - 08:40 AM

<?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++;
}

?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Sam Granger

Sam Granger
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 07 October 2006 - 08:41 AM

Thanks! Works like a dream! :)

#4 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 07 October 2006 - 08:47 AM

Or use foreach

<?php


$files = array("one.jpg","two.jpg","whatever.jpg");

foreach($files as $file)
{
  if(file_exists($file)) echo "$file does exist <br />"; else break;
}


?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users