richard_PHP Posted March 8, 2009 Share Posted March 8, 2009 back again so soon!!! i was just wondering if there was a way to display a message saying 'no files to show' in an if statement when there are no files in a directory. code: <form action="remove.php" method="post" name="list"> <? //define the path as relative $path = "uploads/"; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") echo "<input name='files' type='radio' value='$file' /><a href='uploads/$file' target='_blank'>$file</a><br />"; } //closing the directory closedir($dir_handle); ?> <p><input name="submit" type="submit" value="Delete File" /></p> </form> so if there are no files to list in the directory then display the message: NO FILES FOUND Quote Link to comment https://forums.phpfreaks.com/topic/148466-solved-if-statement/ Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 Use a counter if counter is greater than two, you have files else you have no files because you have ".." and "." . Quote Link to comment https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779551 Share on other sites More sharing options...
Stephen68 Posted March 8, 2009 Share Posted March 8, 2009 Not sure but maybe something like this? EDIT: I change the code and added a else to you if statment, not sure if it would work or not <form action="remove.php" method="post" name="list"> <? //define the path as relative $path = "uploads/"; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop if (!$dir_handle) { while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") echo "<input name='files' type='radio' value='$file' /><a href='uploads/$file' target='_blank'>$file</a><br />"; }else{ echo "No files message"; } } //closing the directory closedir($dir_handle); ?> <p><input name="submit" type="submit" value="Delete File" /></p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779552 Share on other sites More sharing options...
richard_PHP Posted March 8, 2009 Author Share Posted March 8, 2009 EDIT: I change the code and added a else to you if statment, not sure if it would work or not tried that approach already. lol Quote Link to comment https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779563 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 Counter. Quote Link to comment https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779564 Share on other sites More sharing options...
richard_PHP Posted March 8, 2009 Author Share Posted March 8, 2009 <? $counter = $file < 1; //define the path as relative $path = "uploads/"; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") echo "<input name='files' type='radio' value='$file' /><a href='uploads/$file' target='_blank'>$file</a><br />"; if ($counter){ echo "Nothing to see."; } } //closing the directory closedir($dir_handle); ?> Just echos NOTHING TO SEE on the left of each file if there are files there already. EDIT: and when there are no files, its put twice. Quote Link to comment https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779579 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 <? $counter = 0; //define the path as relative $path = "uploads/"; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop while ($file = readdir($dir_handle)) { $counter++; if($file!="." && $file!="..") echo "<input name='files' type='radio' value='$file' /><a href='uploads/$file' target='_blank'>$file</a><br />"; if ($counter===2){ echo "Nothing to see."; } } //closing the directory closedir($dir_handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779582 Share on other sites More sharing options...
richard_PHP Posted March 8, 2009 Author Share Posted March 8, 2009 that displays NOTHING TO SEE just the once ( ), however even if there is a file it displays the message next to the first file. ( ) Quote Link to comment https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779593 Share on other sites More sharing options...
Mark Baker Posted March 8, 2009 Share Posted March 8, 2009 $counter = 0; //define the path as relative $path = "uploads/"; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop while ($file = readdir($dir_handle)) { $counter++; if($file!="." && $file!="..") echo "<input name='files' type='radio' value='$file' /><a href='uploads/$file' target='_blank'>$file</a><br />"; } //closing the directory closedir($dir_handle); } if ($counter == 2){ echo "Nothing to see."; } Quote Link to comment https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779597 Share on other sites More sharing options...
kickstart Posted March 8, 2009 Share Posted March 8, 2009 Hi Or this, which saves playing with the counter. <form action="remove.php" method="post" name="list"> <? //define the path as relative $path = "uploads/"; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop $OutStr = ""; while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") $OutStr .= "<input name='files' type='radio' value='$file' /><a href='uploads/$file' target='_blank'>$file</a><br />"; } //closing the directory closedir($dir_handle); echo (($OutStr) ? $OutStr : "Nothing to see."); ?> <p><input name="submit" type="submit" value="Delete File" /></p> </form> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779600 Share on other sites More sharing options...
richard_PHP Posted March 8, 2009 Author Share Posted March 8, 2009 got it working now. thanks everyone again! ;D Quote Link to comment https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779602 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.