0

Right off the bat I know there are already alot of threads on here about using Glob() to feed a foreach loop. I've read every one I can find and I still have difficulty so I though I would post my code here in the event I am missing something.

My goal: use a foreach loop to wrap HTML pages in some markup and display them on my page. I could have just written the HTML right into my page, but I anticipate alot changes and additions in the next couple of weeks and Im worried about overlapping versions (cant use version control here.)

So far I have this script pulling in the pages and displaying them but IF logic is not triggering. What am I doing wrong here?

<?php

        $directory = 'includes/pages';
        $dirIT = new DirectoryIterator($directory);
        try {        
            // Pull in HTML files from directory    

            foreach ( $dirIT as $key => $item ) {           
                if ($item->isFile()) {

                    if ( $key == '0')
                    {
                        echo "<div class=\"one-third column alpha\">";
                        $path = $directory . "/" . $item;
                        include $path;
                        echo "</div>";
                    }

                    elseif ($key == count ( dirIT ) - '1' )
                    {
                        echo "<div class=\"one-third column omega\">";
                        $path = $directory . "/" . $item;
                        include $path;
                        echo"</div>";
                    }

                    else
                    {
                    echo "<div class=\"one-third column\">";
                    $path = $directory . "/" . $item;   
                    include $path;  
                    echo "</div>";
                    }
                }
            }   
        }   
        catch(Exception $e) {
            echo 'There are no pages to display.<br />';    
        }
    ?>
4
  • Why are you putting quotes around your numbers? Commented Mar 14, 2013 at 15:39
  • Have you tried removing quotes around the numbers in if statement? Because $key will be integer, not string value Commented Mar 14, 2013 at 15:40
  • That is fine as long as he's not using === or !==. Commented Mar 14, 2013 at 15:40
  • Oh whoops I did just have them in there without, but I still was not having success. Thought maybe I have to send them in as a string. Commented Mar 14, 2013 at 15:40

3 Answers 3

2

In the following else if statement, you have a small typo and aren't using an actual variable:

elseif ($key == count ( dirIT ) - '1' )

dirIT should be $dirIT. Try updating to the following and see if it helps:

elseif ($key == (count($dirIT) - 1))

Side-note: Because $dirIT doesn't change inside your loop, you could pre-count it before entering the loop to prevent having to re-count each time. For instance, you can have:

$dirITCount = (count($dirIT) - 1);
foreach ( $dirIT as $key => $item ) { 
    ...
    } else if ($key == $dirITCount) {
Sign up to request clarification or add additional context in comments.

Comments

1

As @newfurniturey says, the problem is caused by a missing $ on the elseif line.

I would like to suggest this rewrite of your code:

$g = glob('includes/pages/*.html');
$c = count($g);
if( $c > 0) {
    foreach ( $g as $key => $item ) {
        echo '<div class="one-third column';
        if( $key == 0) echo ' alpha';
        if( $key == $c-1) echo ' omega';
        echo '">';
        include $item;
        echo '</div>';
    }
}
else {
    echo 'There are no pages to display.<br />';    
}

1 Comment

thank you very much. Your example worked and is far more efficient that my code.
0

Most probably the problem is in your isFile() function. Try to use simple native (http://php.net/manual/en/function.is-file.php) is_file() function to check if particular item is file or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.