I need to display specific value of key 'pasdiz_alus'
from array $form->data
into a table cell. And I need to display this table row only if value of key 'pasdiz_alus'
is greater than '0'
.
The code for this is below, but the problem is that output displays also the value of key 'pasdiz_alus'
above my table row and there it is displayed as many times as number of keys of array.
How can I get rid of this display of value of " 'pasdiz_alus'
x times of number of keys in array (in my case 29 times - there are 29 keys in the array)"? In this case it is: 5454545454545454......
My code is:
<table style="width: 800px;">
<tbody>
<?php
if ($form->data['pasdiz_alus'] > 0){
echo '<tr><td style="width: 100px;">Bilde šeit</td><td style="width: 500px;"> <strong>Pašdizainēts alus</strong></td>';
foreach($form->data as $key => $value) {
if($key === 'pasdiz_alus')
echo '<td style="width: 100px;">';
echo $form->data['pasdiz_alus'];
echo '</td>';
}
echo '<td style="width: 100px;">Cena šeit</td></tr>';
}
?>
</tbody>
</table>
And this is the output display, in this case the value of 'pasdiz_alus'
is 54 The first row is the "wrong" one that I need to get rid off, and the second row is the "right" one.
5454545454545454545454545454545454545454545454545454545454
Bilde šeit Pašdizainēts alus 54 Cena šeit
Thanks for helping! Brgds, Raivis
The problem in your script is here:
if($key === 'pasdiz_alus') // <-- missing opening "{"
echo '<td style="width: 100px;">'; // <-- inside the "if"
echo $form->data['pasdiz_alus']; // <-- OUTSIDE the "if"
echo '</td>'; // <-- OUTSIDE the "if"
} // <-- this matches the foreach "{"
Why do you cycle all the array keys, instead of directly accessing it?
<table style="width: 800px;">
<tbody>
<?php if ($form->data['pasdiz_alus'] > 0) { ?>
<tr>
<td style="width: 100px;">Bilde šeit</td>
<td style="width: 500px;"><strong>Pašdizainēts alus</strong></td>
<td style="width: 100px;"><?=$form->data['pasdiz_alus']?></td>
<td style="width: 100px;">Cena šeit</td>
</tr>
<?php } ?>
</tbody>
</table>
This should solve your problem, but I'd recommend also to move the if
part before even opening the table:
<?php if ($form->data['pasdiz_alus'] > 0) { ?>
<table style="width: 800px;">
<tbody>
<tr>
<td style="width: 100px;">Bilde šeit</td>
<td style="width: 500px;"><strong>Pašdizainēts alus</strong></td>
<td style="width: 100px;"><?=$form->data['pasdiz_alus']?></td>
<td style="width: 100px;">Cena šeit</td>
</tr>
</tbody>
</table>
<?php } ?>
foreach($array as $k => $v)
{
if($k == 'pasdiz_alus' && $v > 0)
{
echo $v;
}
}
you need that addition in your if to make sure its only printed if it is the right key and its greater 0