0

I have a loop within PHP (codeigniter framework) that uses an array ($products) to display product data. Not all the products in the array have values.

Is there anyway that I can hide the call to display those values if they do not exist within the array.

Full code for the loop below;

<!-- Product item row -->    
    <?php foreach($products as $product): ?>  
    <div class="row product-list-row">
    <div class="col-md-2 item-img-container" align="center">
    <img class="img-responsive item-img" src="http://products.supercompare.co.uk<?= $product['logo']?>" alt="<?= $product['name']?> - supacompare.co.uk" width="190px" height="120px" /><br />
    <!--<a role="button" data-toggle="collapse" href="#item-terms" aria-expanded="false" aria-controls="item-terms">LESS DETAILS &#9660</a>-->
    </div>

    <div class="col-md-7 item-text">
    <h3><?= $product['name']?></h3>

    <div class="content-wrapper">
    <div class="content-1">
    <p><strong><?= $product['custom_fields'][0]['field']?></strong>
    <br /><?= $product['custom_fields'][0]['value']?></p>
    </div>  
    <div class="content-2">
    <p><strong><?= $product['custom_fields'][1]['field']?></strong>
    <br /><?= $product['custom_fields'][1]['value']?></p>
    </div>
    <div class="content-3">
    <p><strong><?= $product['custom_fields'][2]['field']?></strong>
    <br /><?= $product['custom_fields'][2]['value']?></p>
    </div>
    <div class="content-4">
    <p><strong><?= $product['custom_fields'][3]['field']?></strong>
    <br /><?= $product['custom_fields'][3]['value']?></p>
    </div>
   <div class="content-bottom">
    <p><?= $product['footer_text']?></p>  
    </div>
    </div>

    </div>

    <div class="col-cta"><a class="apply-btn" href="<?= $product['tracking_link']?>" target="_blank">SEE DEAL &raquo;</a></div>

    <!--<div class="clearfix"></div>-->
    <!--<div id="item-terms" class="col-md-12 footer-terms" style="background-color:#B4B4B4; padding:10px; margin-top:10px;"><?= $product['footer_text']?></div>-->

    </div>
    <?php endforeach; ?>   
2
  • pls add printr($product); result Commented Feb 10, 2016 at 9:57
  • you can use empty or isset to check if the value is empty or the key is set Commented Feb 10, 2016 at 9:58

3 Answers 3

1

You can do that by checking the array for not empty by !empty.

if(!empty($product['custom_fields'][0]['value'])){
//your code
}

According to your code.

<?php if(!empty($product['custom_fields'][0]['value'])){ ?>
    <div class="content-1">
        <p><strong><?= $product['custom_fields'][0]['field']?></strong>
        <br /><?= $product['custom_fields'][0]['value']?></p>
    </div> 
<?php } ?>
Sign up to request clarification or add additional context in comments.

3 Comments

All I am getting is an error when I try to add that code <div class="content-1"> <p><strong><?php if(!empty($product['custom_fields'][0]['value']) ['custom_fields'][0]['field']?></strong> <br /><?= $product['custom_fields'][0]['value']?></p> </div>
I am using the updated answer thanks. I get this error - Parse error: syntax error, unexpected '{' - could it be because I'm using it within a foreach statement?
Glad to know that.. Welcome
0

here is an example :

<?php
if(isset($product['custom_fields'][0]['value']) && $product['custom_fields'][0]['value'] !=""){
//your code
}
?>

Comments

0
foreach ($products as $product) {
    foreach ($product['custom_fields'] as $value) {
        if (!$value) {
            continue 2;
        }
    }
    //your code here!!!
}

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.