cakephp - Two PHP foreaches causing repeating of rows -


hey guys, have bit of problem , it's been frustrating me no end. i've got cakephp application i've been working on, , can't seem find way display things way i'd to.

this code in controller method:

function index(){     $this->order->recursive = 2;     $orders = $this->order->findallbyvendor_id($this->auth->user('vendor_id'));     foreach($orders $order){         $item_ids = explode(',', $order['order']['items']);         foreach($item_ids $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')))); } 

this view code:

<div class="block" id="vendor-dash">     <table cellpadding="0" cellspacing="0">         <?php         $i = 0;         foreach($orders $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 $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 $order_product){                         echo $order_product . ', ';                     }             ?></td>             <td><a href="#" id="<?php echo $order['order']['id']; ?>" class="action">fulfill order</a></td>         </tr>         <?php                     }          } ?> </div> 

both foreach($orders $order) , foreach($orders_products $order_products) holding info multiple orders. $orders hold order number, pickup time, etc. while $orders_products array of arrays created in controller (above) "exploding" array of product id's stored in database field each order (e.g. 1,2,3).

whenever page brought up, however, displays each order 3 times because of 2 foreach loops run 1 time each order. instead of 3 orders few items each, end seeing 9 orders.

any ideas?

it looks in index method you're not setting $order_products empty array before looping on $item_ids -- try this:

function index(){     $this->order->recursive = 2;     $orders = $this->order->findallbyvendor_id($this->auth->user('vendor_id'));     foreach($orders $order){         $order_products = array(); // here reset empty array.         $item_ids = explode(',', $order['order']['items']);         foreach($item_ids $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')))); } 

Comments

Popular posts from this blog

Delphi Wmi Query on a Remote Machine -