Jump to content

Recommended Posts

back again so soon!!! :D

 

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

Link to comment
https://forums.phpfreaks.com/topic/148466-solved-if-statement/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779552
Share on other sites

<?
            $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.

Link to comment
https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779579
Share on other sites

<?
$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);
         ?>

Link to comment
https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779582
Share on other sites

$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.";
}

 

Link to comment
https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779597
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/148466-solved-if-statement/#findComment-779600
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.