PHP foreach with conditional -


<? foreach ($cases $case) :?> <? if (isset($case['case_status']) && ($case['case_status'] == 'incomplete')) :?> <div>     <div class="alert alert-error">             <?= $warning ?>     </div> 

i'm trying have foreach print out div once if condition met. problem is, every case meets condition div printed.

i have tried using in_array, don't think understand syntax. in_array correct? there better way this?

you can use flag mark, have printed div , add condition if:

<?  $flag = false; foreach ($cases $case) :?> <? if (isset($case['case_status'])         && ($case['case_status'] == 'incomplete')        && ($flag == false)) :?> <div>     <div class="alert alert-error">             <?= $warning ?>     </div>     <?php $flag = true; ?> 

or if don't need in loop, can break after first encountered element.

<?  foreach ($cases $case) :?> <? if (isset($case['case_status']) && ($case['case_status'] == 'incomplete')) :?> <div>     <div class="alert alert-error">             <?= $warning ?>     </div>     <?php break; ?> 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -