Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
两个PHP foreaches导致行重复_Php_Cakephp - Fatal编程技术网

两个PHP foreaches导致行重复

两个PHP foreaches导致行重复,php,cakephp,Php,Cakephp,嘿,伙计们,我有点问题,这一直让我沮丧不已。我一直在开发一个CakePHP应用程序,但我似乎找不到一种方式来显示我想要的东西 这是我的控制器方法中的代码: function index(){ $this->Order->recursive = 2; $orders = $this->Order->findAllByVendor_id($this->Auth->User('vendor_id')); foreach($orders as

嘿,伙计们,我有点问题,这一直让我沮丧不已。我一直在开发一个CakePHP应用程序,但我似乎找不到一种方式来显示我想要的东西

这是我的控制器方法中的代码:

function index(){
    $this->Order->recursive = 2;
    $orders = $this->Order->findAllByVendor_id($this->Auth->User('vendor_id'));
    foreach($orders as $order){
        $item_ids = explode(',', $order['Order']['items']);
        foreach($item_ids as $item_id){
            $products = $this->Product->findById($item_id);
            $order_products[] = $products['Product']['product_name'];
        }
        $vendor_orders[] = $order_products;
    }
    $this->set('orders_products', $vendor_orders);
    $this->set('orders', $this->paginate('Order', array('Order.vendor_id' => $this->Auth->User('vendor_id'))));
}
这是我的视图代码:

<div class="block" id="vendor-dash">
    <table cellpadding="0" cellspacing="0">
        <?php
        $i = 0;
        foreach($orders as $order){
            $class = null;
            if ($i++ % 2 == 0) {
                $class = ' class="altrow"';
            }

            $pickup_time=date("H:i", strtotime($order['Order']['pickup_time']));
            $difference = strtotime('23:52') - date('H:i');
            // echo date("H:i", strtotime($difference));
            foreach($orders_products as $order_products){
        ?>
        <tr<?php echo $class;?> id="<?php echo $order['Order']['id']; ?>">
            <td class="confirmation"><?php echo $order['Order']['confirmation']; ?></td>
            <td class="products">
            <?php 
                    foreach($order_products as $order_product){
                        echo $order_product . ', ';
                    }
            ?></td>
            <td><a href="#" id="<?php echo $order['Order']['id']; ?>" class="action">Fulfill Order</a></td>
        </tr>
        <?php    
                } 
        } ?>
</div>


索引
方法中,在循环返回
$item\u id
之前,似乎没有将
$order\u products
设置为空数组--请尝试以下操作:

function index(){
    $this->Order->recursive = 2;
    $orders = $this->Order->findAllByVendor_id($this->Auth->User('vendor_id'));
    foreach($orders as $order){
        $order_products = array(); // Here is the reset to an empty array.
        $item_ids = explode(',', $order['Order']['items']);
        foreach($item_ids as $item_id){
            $products = $this->Product->findById($item_id);
            $order_products[] = $products['Product']['product_name'];
        }
        $vendor_orders[] = $order_products;
    }
    $this->set('orders_products', $vendor_orders);
    $this->set('orders', $this->paginate('Order', array('Order.vendor_id' => $this->Auth->User('vendor_id'))));
}

阵列过度生长。在控制器中,$order_products数组随着每个循环而增长

因此,在
foreach($orders as$order)
中重置数组:

foreach($orders作为$order){
$item_id=explode(“,”,$order['order']['items']);
$order_products=array();//产品->findById($item_id);
$order_products[]=$products['Product']['Product_name'];
}
$vendor\u orders[]=$order\u产品;
}
而且,有些东西看起来很奇怪。。。检查

…之后的额外foreach循环

  //echo date("H:i", strtotime($difference));
  foreach($orders_products as $order_products){  // <--  EXTRA. comment it out and it's }
//回显日期(“H:i”,strotime($difference));

foreach($orders\u products作为$order\u products){//您是否看到每个订单中预期的“订单产品”数量?或者是这些数量的倍数?是的。正确的订单产品数量出现了,每个订单只出现了三次(a、b、c、a、b、c、a、b、c)因为我有两个foreach循环。你有什么想法吗?你可能还想在
订单
产品
之间创建一个
HABTM
,这样你就不必做逗号分隔的列表。查看如何设置HABTM。我是新手,所以这可能不是时间密集型项目的最佳技术选择,但是嘿,总得学点什么,对吧?:)用你提供的代码替换了控制器代码,但仍然重复命令。一定是视图中有什么东西?添加了新的视图建议。谢谢!没有立即修复,但我认为我现在走对了方向。:)
  //echo date("H:i", strtotime($difference));
  foreach($orders_products as $order_products){  // <--  EXTRA. comment it out and it's }