php - store arrays in variables inside foreach -
php - store arrays in variables inside foreach -
sorry if title not clear need this:
i have foreach printing info db , have multiple variables should store arrays , print main variable others variables.
foreach ($aitems $i => $aiteminfo) { $var1 = ''; $var2 = ''; $var3 = ''; $var1 .= $aiteminfo['val1']; $var2 .= $aiteminfo['val2']; $var3 .= $aiteminfo['val3']; // operations db info if ($var1 > 0) { if ($var1 == 1 && $var2 == 0) { $a = 'some text.'; } else if ($var1 == 1 && $var2 == 1) { $a = 'some other text.'; } else if ($var1 > 1 && $var2 == 0) { $a = 'some new text.'; } else if ($var1 > 1 && $var2 == 1) { $a = 'some other new text.'; } $new_var = ''; // html code $new_var .= '<div id=""> <a id="">' . $othervar . '</a> </div> <div id=""> <span id="">' . $a . '</span> </div>'; } if ($var > 0) { // html code $other_var .= ' . $vars . '; } // html construction $main_var .= ''; }
and print main variable:
<div class=""> <?php echo $main_var; ?> </div>
actually variables same previous. tried placing dot before "=" have no thought how works or for...
what i'm doing wrong? right way of create each variable can store each value of arrays?
you not using .=
concatenation in case, mess if conditions made.
initial:
$var1 = '1'; if($var == 1) { // okay status }
second iteration, declaration 1 time again within loop. not indend do:
just create this:
$main_var = $new_var = ''; // initialize output outtop foreach ($aitems $i => $aiteminfo) { if ($aiteminfo['val1'] == 1 && $aiteminfo['val2'] == 0) { $a = 'some text.'; } else if ($aiteminfo['val1'] == 1 && $aiteminfo['val2'] == 1) { $a = 'some other text.'; } // html code $new_var .= ' <div id=""> <a id="">' . $othervar . '</a> </div> <div id=""> <span id="">' . $a . '</span> </div> '; if ($aiteminfo['val1'] > 0) { // html code // process here whatever } // html construction $main_var .= $new_var; }
php html
Comments
Post a Comment