?
Solved

having trouble displaying message outside of foreach loop

Posted on 2014-11-11
4
Medium Priority
?
104 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

771 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