Page 1 of 1

Break WHILE with readdir

Posted: Mon Dec 05, 2011 2:24 am
by Pazuzu156
I have an image directory for user images. And when they want to change pictures, it shows the 4 most recent ones. How do I limit this with my code:

Code: Select all

<?php
$imgDir = "./ppics/".$fname.$lname;
$handler = opendir($imgDir);
$ignore = array('.','..','Thumbs.db');
while($file = readdir($handler)) {
	if(!in_array($file,$ignore)) {
		echo '<a href="./assets/changePic.php?img='.$file.'" name="pic_'.$file.'"><img src="'.$imgDir.'/'.$file.'" alt="image_'.$file.'" style="width:50px;"></a> ';
	}
}
?>

Re: Break WHILE with readdir

Posted: Mon Dec 05, 2011 5:14 am
by social_experiment
If i understand correctly you only want to show 1 image: adding break might do it

Code: Select all

<?php
if(!in_array($file,$ignore)) {
                echo '<a href="./assets/changePic.php?img='.$file.'" name="pic_'.$file.'"><img src="'.$imgDir.'/'.$file.'" alt="image_'.$file.'" style="width:50px;"></a> ';
        }
// add a break statement
break;
?>

Re: Break WHILE with readdir

Posted: Mon Dec 05, 2011 2:53 pm
by Pazuzu156
I want to show 5, then break.

Re: Break WHILE with readdir

Posted: Tue Dec 06, 2011 9:47 am
by pickle
Add a counter. break when the counter reaches 5.