Solved

having trouble displaying message outside of foreach loop

Posted on 2014-11-11
4
83 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 108

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP Curl Setup Option 4 33
Magneto product importing through API script 10 32
Time difference 10 35
Paging Using PHP 7 34
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 look for a specific file type in a local or remote server directory using PHP.

759 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

22 Experts available now in Live!

Get 1:1 Help Now