0

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

3
  • 3
    I don't see anything wrong in particular with the code you've posted. Perhaps it happens earlier? Commented Aug 22, 2012 at 10:48
  • 1
    Agreed with Jack. do a print_r($form->data) and paste it please. Commented Aug 22, 2012 at 10:49
  • 1
    is this the whole file?seems like uncommented echo or print. Commented Aug 22, 2012 at 10:49

2 Answers 2

4

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 } ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Well spotted! Didnt see that missing curly
Thanks @Matteo Tassinari it works now! And thanks for explanation, I got it.
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.