Steven Walker
Steven Walker

Reputation: 412

What is wrong with my if statement?

I had the below code which worked fine until I added an if statement to restrict the loop to only run on certain items in the array. I am now only getting a blank page which suggests there is an error somewhere in my code since adding the if statement, but I can't figure out where.

I'd really appreciate any help on solving this, as well as suggestions on how I could have solved myself (I'm still new to PHP and not sure how to effectively debug this type of issue).

Nb. There is an opening <?php tag not shown in the below snippet.

foreach ($portfolioProjects as $portfolio) {
if ($portfolio['featureContent'] == "Yes") {
    ?>
    <div class="row featurette">
        <?php
        //for each odd number, change the layout so it looks nice :)
        if ($loopCount % 2 == 0) {
            echo '<div class="col-md-7">';
        } else {
            echo '<div class="col-md-7 col-md-push-5">';
        }
        ?>
        <h2 class="featurette-heading"><?php echo $portfolio[title]; ?> <span class="text-muted"><?php echo $portfolio[languages]; ?></span> 
            <?php
            //Check array for newTag which will be added to show a tag to the user
            if ($portfolio[newTag] == "Yes") {
                echo '<span class="label label-success test pull-right"> New!</span>';
            }
            ?></h2> 
        <p class="lead"><?php echo $portfolio[blurb]; ?></p>
    </div><!--end of column1-->
    <?php
    if ($loopCount % 2 == 0) {
        echo '<div class="col-md-5">';
    } else {
        echo '<div class="col-md-5 col-md-pull-7">';
    }
    ?>
    <img class="featurette-image img-responsive center-block" data-src="holder.js/200x200/auto" alt="200x200" src="assetts/200x200.gif" data-holder-rendered="true">
    </div>
    </div>
    <?php
    //if statement to stop divider being added if last item in array
    $loopCount = $loopCount + 1;
    if ($loopCount != $itemsCount) {
        echo '<hr class="featurette-divider">';
    }
}

} ?>

Upvotes: 0

Views: 37

Answers (2)

Aakash Martand
Aakash Martand

Reputation: 944

You forgot to wrap array keys in quotes

`$portfolio['title']  
$portfolio['languages']  
$portfolio['newTag']  
$portfolio['blurb']`

Upvotes: 0

pappfer
pappfer

Reputation: 181

The easiest way to find out what the problem is, to check your webserver's log file, or turn on error_reporting and display_errors in your php.ini file.

Are newTag and blurb constants or keys in the $portfolio array? If they are keys, you should use apostrophes.

if ($portfolio['newTag'] == "Yes") {

and

<p class="lead"><?php echo $portfolio['blurb']; ?></p>

Upvotes: 1

Related Questions