Solved

having trouble displaying message outside of foreach loop

Posted on 2014-11-11
4
97 Views
Last Modified: 2014-11-11
Hi

I have some working code, thanks to some help from EE where a directory is scanned and the 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 foreach 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" 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;
?>

Open in new window

0
Comment
Question by:peter-cooper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40434755
Something like this... Look for the word "data" in the code to see what we're doing here.

<?php
error_reporting(E_ALL);

    // 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();

    // SET AN INDICATOR OF WHETHER WE FOUND DATA
    $data_found = FALSE;
    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;

        // SET AN INDICATOR TO SHOW WE FOUND DATA
        $data_found = TRUE;
        
        // 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;
    }

    // DID WE FIND ANY DATA
    if ($data_found === FALSE)
    {
        // NO DATA
        echo "NO DATA";
        die();
    }
    // 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;

Open in new window

0
 

Author Comment

by:peter-cooper
ID: 40434782
@Ray

Line 12 seems to be breaking my layout. If I comment that line out, my divs display as usual.  If I leave that line in  and comment lines 49-55 in your code my divs display as usual. I copied your code exactly. Not sure whats happening here. Thanks
0
 

Author Comment

by:peter-cooper
ID: 40435317
@Ray

If I code on it's own page it works fine. Guess there must be something on my main page that throws the layout out of sync when I use your code. Anyway,accept the points because your code does exactly what it says on the tin :-) many thanks
0
 

Author Closing Comment

by:peter-cooper
ID: 40435320
Many thanks once again.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question