dongzhi4073 2012-08-22 10:43
浏览 26
已采纳

仅显示数组的特定值

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

  • 写回答

2条回答 默认 最新

  • douxi3977 2012-08-22 10:50
    关注

    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 } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算