我有一些工作代码,其中扫描目录并将PDF文件结果放在表格中。但是,我似乎无法做的是,如果没有结果,则在while循环外的表中显示一条消息。有人可以帮忙吗?感谢
<?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" style="margin-bottom: 15px; margin-top:15px;"><br /><a href="'
. $sub
. '/'
. $nom
. '" style="color:#0099FF; text-decoration:none; font-size:12px; font-family: Verdana, Geneva, sans-serif;">'
. $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%" style="padding-bottom:20px !important;">$td1</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td2</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td3</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$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" style="border:1px solid grey; padding-bottom: 10px; margin-bottom:20px;">
<th style="text-align:center; padding: 10px !important; padding-bottom: 20px; border:1px solid black; background-color: #3399FF; color: white; font-size: 18px !important;" colspan="4">Destruction Certificates Download</th>
<tr>
<th style="text-align:center; padding-top: 10px !important;"></th>
<th></th>
<th></th>
<th></th>
</tr>
$trs
</table>
EOD;
// SHOW THE WORK PRODUCT
echo $tab;
?>
答案 0 :(得分:1)
尝试在WH语句中包含WHILE LOOP以验证是否有任何要循环的内容。
If(!empty($sss)){
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%" style="padding-bottom:20px !important;">$td1</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td2</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td3</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td4</td>
</tr>
EOD;
// APPEND THE TABLE ROW TO THE OTHER ROWS
$trs .= $tr;
}
}
else{
$msg = " Put some message to display that there are no files to display ";
$tr = <<<EOD
<tr>
<td align="center" colspan="4" style="padding-bottom:20px !important;">$msg</td>
</tr>
EOD;
$trs .= $tr;
}