【问题标题】:No foreach Display没有 foreach 显示
【发布时间】:2021-07-08 11:02:07
【问题描述】:
    <?php
        $pros_array_id= array_column($_SESSION['cart'], 'paint_id');
    
        $productee = $productn->getData('paint');

            foreach($productee as $pro):

                if($pro['paint_id'] == $pros_array_id):
    ?>
       <table>
        <tr>
            <td class="imgTag">
                <a href="#"><img class="img-fluid" src="<?php echo $pro['paint_image'] ?? 1; ?>" ></a>
            </td>
        </tr> 
        </table>  
 
    <?php
        endif;
        endforeach;
    ?>
Am trying to display session cart items and its not showing anything. When I print_r($productee) and print_r($pros_array_id) after the foreach statement both display the accurate data, yet nothing displat in the <tr> tag.

是显示结果 当我像这样内爆 $pros_array_id 时 "$imp = implode(" ",$pros_array_id);"并将变量放在 if 语句中,如果会话中只有一种产品,它可以正常工作,但是当我在会话中添加多个产品时,什么都不会再次显示。 请有人指出我应该怎么做? 谢谢

【问题讨论】:

    标签: php loops foreach


    【解决方案1】:

    试试这样:

    <table>
    <?php
      $pros_array_id = array_column($_SESSION['cart'], 'paint_id');
      $productee = $productn->getData('paint');
    
      foreach($productee as $pro) {
        //Also see and try to disable this check to see if it works then!
        if($pro['paint_id'] == $pros_array_id) { ?>
        <tr>
          <td class="imgTag">
            <a href="#"><img class="img-fluid" src="<?php echo $pro['paint_image'] ?? 1; ?>"></a>
          </td>
        </tr>
    <?php 
        }
      }  
    ?>
    </table>
    

    始终确保在调试此类代码时消除任何可能导致错误的检查,因此也尝试禁用“if 子句”,看看它是否会显示一些内容。

    【讨论】:

    • 它仍然没有显示任何内容。 .当我在 if 语句之后 print_r() 没有打印出来。当我在 foreach 语句之后 print_r($pro) 显示了绘图表中的所有项目,但是当我 print_r(pros_array_id) 作为会话购物车项目时,它显示了这个“Array ( [0] => 7 ) Array ( [0] => 7 ) 数组 ( [0] => 7 ) 数组 ( [0] => 7 ) 数组 ( [0] => 7 ) 数组 ( [0] => 7 ) 数组 ( [0] => 7 ) ",但在 标签中没有显示。
    • 所以$pros_array_id的数据处处等于'7'?因为这表明您的 array_column($_SESSION['cart'], 'paint_id'); 根本不起作用。 $_SESSION['cart'] 的值是多少?
    • 很抱歉重复了,我的回车键卡住了。值为数组( [0] => 7 )。并且 $_SESSION['cart'] 是 Array ( [0] => Array ( [paint_id] => 7 ) )
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签