I have a model with this function
function dummy_system(){
$this->db->select('SKU, size, color')
->from('order_detail')
->where('order_id',"52F4CC00C32");
$query = $this->db->get();
// 2nd query
foreach($query->result() as $row):
$SKU = $row->SKU;
$size = $row->size;
$color = $row->color;
$this->db->select('stock_quantity')
->from('item_stock')
->join('items','items.stock_id = item_stock.stock_id')
->join('item','item.item_id = items.item_id')
->where('item.SKU',$SKU)
->where('item_stock.size',$size)
->where('item_stock.colour',$color);
$query = $this->db->get();
endforeach;
if($query->num_rows() > 0){
return $query->result();
}
else{
return array();
}
}
this controller
public function index()
{
$this->load->model('nekogear');
$data['dummy'] = $this->nekogear->dummy_system();
echo "<pre>";
die(print_r($data, TRUE));
}
and this is the result which only showing the last query.
Array
(
[dummy] => Array
(
[0] => stdClass Object
(
[stock_quantity] => 5
)
)
)
my question is how to do the foreach loop in model to get all query result with one order_id? (the current order_id has two rows with same order_id) with this code (which I need the SKU, size & color values into the next queries)
$this->db->select('SKU, size, color')
->from('order_detail')
->where('order_id',"52F4CC00C32");
$query = $this->db->get();
if($query->num_rows() > 0){
return $query->result();
}
else{
return array();
}
it can fetch the result
Array
(
[dummy] => Array
(
[0] => stdClass Object
(
[SKU] => Nisekoi-01
[size] => L
[color] => Biru
)
[1] => stdClass Object
(
[SKU] => Wooser-01
[size] => XL
[color] => Kuning
)
)
)
with that in mind, I think the problem lies in the foreach loop inside my model and I don't know how to solve this.
thanks for your time and sorry for my bad english.
$this->db->last_query();in your foreach to see what queries it's trying to make. You might also try callingenable_profiler();. I think it outputs your queries. This of course will just help you debug your queries and see if they are actually being called correctly. - Joseph Mariklestock_quantityFROM (item_stock) JOINitemsONitems.stock_id=item_stock.stock_idJOINitemONitem.item_id=items.item_idWHEREitem.SKU= 'Wooser-01' ANDitem_stock.size= 'XL' ANDitem_stock.colour= 'Kuning' ) - ceroberoz