我有一个具有此功能的模型
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();
}
}
这个控制器
public function index()
{
$this->load->model('nekogear');
$data['dummy'] = $this->nekogear->dummy_system();
echo "<pre>";
die(print_r($data, TRUE));
}
,这是仅显示最后一个查询的结果。
Array
(
[dummy] => Array
(
[0] => stdClass Object
(
[stock_quantity] => 5
)
)
)
我的问题是如何在模型中执行foreach循环以获得一个order_id的所有查询结果? (当前的order_id有两行,order_id相同) 使用此代码(我需要SKU,尺寸和颜色值到下一个查询中)
$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();
}
它可以获取结果
Array
(
[dummy] => Array
(
[0] => stdClass Object
(
[SKU] => Nisekoi-01
[size] => L
[color] => Biru
)
[1] => stdClass Object
(
[SKU] => Wooser-01
[size] => XL
[color] => Kuning
)
)
)
答案 0 :(得分:0)
您可以构建一个数组并循环遍历它以获取股票:
$this->db->select('SKU, size, color')
->from('order_detail')
->where('order_id',"52F4CC00C32");
$query = $this->db->get();
$skus = array();
foreach($query->result() as $row):
$skus[$row->SKU] = array('sku' => $row->SKU, 'size' = $row->size, 'color' = $row->color);
endforeach;
$return = array();
foreach($skus as $sku => $data):
$SKU = $data['sku'];
$size = $data['size'];
$color = $data['color'];
// same query
$return[] = $query->result();
endforeach:
return $return;