Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing PHP to display a folders contents, but show a message when folder is empty
    primarykey
    data
    text
    <p>I'm creating a intranet for my workplace and have used a bit of php I found online to scan the contents of the folder it's in and display them as links. It does this fine, but when it's inside an empty folder I would like it to display a message such as "There are no records matching those criteria.".</p> <p>Is there a way to add something to the php to specify <em>if there are no folders listed print this</em>?</p> <p>I have next to no knowledge of php, but html and css are no problem.</p> <p>Here's the php I'm using in the page:</p> <pre><code>&lt;?php $dir=opendir("."); $files=array(); while (($file=readdir($dir)) !== false) { if ($file != "." and $file != ".." and $file != "A.php") { array_push($files, $file); } } closedir($dir); sort($files); foreach ($files as $file) print "&lt;div class='fileicon'&gt; &lt;a href='$file'&gt; &lt;img src='../../../images/TR-Icon.png'&gt; &lt;p class='filetext'&gt;$file&lt;/p&gt; &lt;/a&gt; &lt;/div&gt;"; ?&gt; </code></pre> <p>If you need anymore code such as the full page html or css just let me know.</p> <p>Thanks in advance for any help.</p> <p>EDIT:</p> <p>After trying Josh's solution it pretty much nailed it, but I'm now getting "No files found" printing 3 times. Here's the code I'm using now:</p> <pre><code>&lt;?php $dir=opendir("."); $files=array(); while (($file=readdir($dir)) !== false) { if( count($files) == 0 ) { echo '&lt;p&gt;No files found&lt;/p&gt;'; } else { if ($file != "." and $file != ".." and $file != "A.php") { array_push($files, $file); } } } closedir($dir); sort($files); foreach ($files as $file) print " &lt;div class='fileicon'&gt; &lt;a href='$file'&gt; &lt;img src='../../../images/TR-Icon.png'&gt; &lt;p class='filetext'&gt;$file&lt;/p&gt; &lt;/a&gt; &lt;/div&gt;"; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload