Solved

having trouble displaying message outside of foreach loop

Posted on 2014-11-11
4
88 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
  • 3
4 Comments
 
LVL 109

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

813 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now