Отображение сообщения вне цикла while

0

У меня есть рабочий код, в котором сканируется каталог, и результаты 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;
?>
Теги:

1 ответ

1
Лучший ответ

Попробуйте включить WHILE LOOP в инструкции IF, чтобы проверить, есть ли что-либо для цикла.

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;

     }
  • 0
    Спасибо за это. однако ошибок нет, но он не отображает $ msg. Тр пуст.
  • 0
    Виноват. Работает сейчас. Спасибо
Показать ещё 1 комментарий

Ещё вопросы

Сообщество Overcoder
Наверх
Меню