php - Displaying message outside of while loop
149
I have some working code where a directory is scanned and the PDF file results placed in a table. However, what I cannot seem to do is if there are no results then display a message in the table outside the while loop. Can someone help with this. Thanks
<?php
// WHAT IS IN THE STORAGE DIRECTORY
$sub = 'destcerts' . '/' . $_SESSION['kt_idcode_usr'];
// READ THE NAMES OF FILES IN THE SUB-DIRECTORY
$fff = new DirectoryIterator($sub);
$sss = array();
foreach ($fff as $filedata)
{
// SKIP THE "DOT" FILES
if ($filedata->isDot()) continue;
// ACTIVATE THIS LINE TO RESTRICT IT TO PDF FILES ONLY
if ($filedata->getExtension() != 'pdf') continue;
// CREATE LINKS TO THESE FILES
$nom = $filedata->getFilename();
$value = substr ($nom, 0, 4);
//$_SESSION['value'] = $value;
if($_SESSION['kt_idcode_usr'] == $value) {
$lnk
= '<img src="destcerts/PDF_icon_100.png" ><br /><a href="'
. $sub
. '/'
. $nom
. '" >'
. $nom
. '</a>'
;
}
// COLLECT THE LINKS HERE
$sss[] = $lnk;
}
// ACCUMULATE THE TABLE ROWS HERE
$trs = NULL;
// COLLECT GROUPS OF FOUR
while (!empty($sss))
{
$td1 = array_shift($sss) or NULL;
$td2 = array_shift($sss) or NULL;
$td3 = array_shift($sss) or NULL;
$td4 = array_shift($sss) or NULL;
// USE HEREDOC TO INSERT THESE INTO A TABLE ROW
$tr = <<<EOD
<tr>
<td align="center" width="20%" >$td1</td>
<td align="center" width="20%" >$td2</td>
<td align="center" width="20%" >$td3</td>
<td align="center" width="20%" >$td4</td>
</tr>
EOD;
// APPEND THE TABLE ROW TO THE OTHER ROWS
$trs .= $tr;
}
// USE HEREDOC TO INSERT THE TABLE ROWS INTO THE TABLE
$tab = <<<EOD
<table id="pdfDownload" width="94%" align="center" border="1" cellspacing="10" cellpadding="0" >
<th colspan="4">Destruction Certificates Download</th>
<tr>
<th ></th>
<th></th>
<th></th>
<th></th>
</tr>
$trs
</table>
EOD;
// SHOW THE WORK PRODUCT
echo $tab;
?>
Answer
Solution:
Try enclosing the WHILE LOOP in a IF statement to verify if there is anything to loop.