0

I'm struggling with making an if statement where if a variable value (the page title) equals 'loan' a field is div is hidden but if the variable equals any other value the div is displayed.

<?php 
    if(!empty($product['custom_fields'][1]['value'])){ ?>
        <div class="content-2">
            <p><strong><?= $product['custom_fields'][1]['field']?></strong>
            <br /><?= $product['custom_fields'][1]['value']?></p>
        </div> 
    <?php } 
?>

I have created the following code to show the hidden div (on on the 'loan' pages) but can't seem to successfully incorporate that code into the if statement for the custom field variables above.

<?php
    if(false !== stripos ($product_list_data['name'], 'loan')){ 
        echo "<div class='content-bottom'><p><strong>" . $product['custom_fields'][1]['field'] . "</strong>
<br />" . $product['custom_fields'][1]['value'] . "</p></div>";
    } 
?>

Any help would be greatly appreciated.

Tommi Halonen
  • 258
  • 1
  • 7
Ben Yates
  • 75
  • 1
  • 11
  • I'm not sure if this fixes the problem, but could you remove `false !==` from the if statement? – Max Feb 18 '16 at 10:05
  • I am not sure what you are trying to say above, what exactly is the problem that you are facing and secondly i think that you have syntax error try this: html code to run if condition is true html code to run if condition is false – Ammar Ajmal Feb 18 '16 at 10:05
  • @AmmarAjmal His syntax is not the problem. – Max Feb 18 '16 at 10:06
  • Please use: [PHP offers an alternative syntax for some of its control structures;](http://php.net/manual/en/control-structures.alternative-syntax.php). In each case, the basic form of the alternate syntax is to change the __opening brace_ to a __colon (:)_ and the _closing brace_ to _`endif` etc._ – Ryan Vincent Feb 18 '16 at 10:08

1 Answers1

2
$display = 'block';
if (strpos($product_list_data['name'], 'loan') !== false) {
    $display = 'none';
}

<div class="content-2" style="display:<?php echo $display; ?>;">
<p><strong><?= $product['custom_fields'][1]['field']?></strong>
<br /><?= $product['custom_fields'][1]['value']?></p>
</div>
Sourav Basak
  • 486
  • 1
  • 4
  • 15
  • You could even change `` to `=$display ?>` as PHP template engine supports that – isnisn Feb 18 '16 at 10:11
  • short tag has some drawbacks. Please view this link: http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – Sourav Basak Feb 18 '16 at 11:02