i have two foreach loop to display some data,
but i want to use a single foreach on basis of database result.
means if there is any row returns from database then
forach($first as $fk=>$fv) should execute otherwise
foreach($other as $ok) should execute.
i m unsing below ternary operator which gives parse error
$n=$db->numRows($taskData); // databsse results
<?php ($n) ? foreach ($first as $fk=>$fv) : foreach ($other as $ok)
{ ?>
<table><tr><td>......some data...</td></tr></table>
<?php } ?>
please suggest me how to handle such condition via ternary operator or any other idea.
Thanks
Use the conditional operator to select the array to use and then use
$fvas the value rather than$okfor the second option.Having two totally different loops being selected by a condition and with totally different variables would not really be a good idea as it would lead to really confusing code as you wouldn’t be sure without checking
$nwhich of$fk,$fvand$okwere set.If you do need to do something different depending on the condition maybe the sections of code that are the same into functions would be a better idea?