从模型到控制器的组合和返回数组 - CodeIgniter
问题描述:
我想结合来自两个单独查询的两个数组。我将我的产品存储在两个独立的表格中:一个用于一般信息,如名称,说明,价格等;另一个用于它们的各种细节,即服装物品的尺寸和颜色。从模型到控制器的组合和返回数组 - CodeIgniter
所以,我的数据库的结构是这样的产品:
products table:
p_id | title | descr | etc
product_attrs table:
attr_id | products.p_id | name | value
其中名称和值可以是名称=大小值=大
如果我试图让所有的细节的产品出来在一个查询,例如:
this->db->select('products.title,
p.description,
p.price,
p.stock,
p.name,
p.value');
$this->db->from('p');
$this->db->where('p.p_id', $id);
$this->db->join('product_attrs', 'product_attrs.product_id = p.p_id', 'inner');
$result = $this->db->get();
return $result->result_array();
我得到填充名称/值对有在该产品的表product_attributes数量的数组。因此,如果有说一个产品的5个属性,我会得到一切回到5次这样的:
Array ([0] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => Brand [value] => Modestly Active Swimwear) [1] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => Colour [value] => Black and Light Blue) [2] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => size [value] => small) [3] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => size [value] => medium) [4] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => size [value] => large))
所以我决定将单独的查询每个表,这样我就可以得到一个结果集的每个。但我想将它们结合起来,这样我就可以将数据作为一个数组返回给Controller,并将其显示到我的视图上。这就是我查询的两个表,我只是需要一种方法来结果结合起来,既:
$this->db->select('p.title,
p.description,
p.price,
p.stock');
$this->db->from('p');
$this->db->where('p_id', $id);
$result = $this->db->get();
$this->db->select('name, value');
$this->db->from('product_attrs');
$this->db->where('p_id', $id);
$result2 = $this->db->get();
如果有人能帮我会非常感激。谢谢
编辑:
我看array_merge()函数现在在php.net,但如果我这样做:
$result = $this->db->get();
$array1 = $result->result_array();
$result2 = $this->db->get();
$array2 = $result2->result_array();
$data = array_merge($array1,$array2);
return $data;
我得到一个以上的阵列:
Array ([0] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20) [1] => Array ([name] => Brand [value] => Modestly Active Swimwear) [2] => Array ([name] => Colour [value] => Black and Light Blue) [3] => Array ([name] => size [value] => small) [4] => Array ([name] => size [value] => medium) [5] => Array ([name] => size [value] => large))
有没有办法在我的视图中取出上述数组中的值?
答
另一种选择是循环遍历第二个结果数组并将值添加到第一个数组。
// Get products results as an array
$this->db->select('products.title,
products.description,
products.price,
products.stock');
$this->db->from('products');
$this->db->where('product_id', $id);
$product = $this->db->get()->result_array();
// Get product attributes as an array
$this->db->select('name, value');
$this->db->from('product_attributes');
$this->db->where('product_id', $id);
$product_attributes = $this->db->get()->result_array();
// Loop through the results
foreach($product_attributes as $attribute) {
// Add results to original product array
$product[$attribute['name']] = $attribute['value'];
}
这将产生一个这样的数组:
[title] => Modest Swimsuit - Full body
[description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant
[price] => 59.95
[stock] => 20
[Brand] => Modestly Active Swimwear
[Colour] => Black and Light Blue
[size] => small
+0
这是一个更清洁的结果,我会试试看。谢谢你的帮助 :) – a7omiton 2013-03-01 07:52:40
我想我已经得到了它。我使用函数array_merge(),然后在我看来我可以得到的值如下:echo $ product_details [0] ['title'] ;.感谢上帝的var_dump! – a7omiton 2013-02-28 22:06:50
OP试图编辑答案:表的前一个名称是非常通用的,所以我没有看到任何问题。你没有编辑所有的事情吗?你编辑你的问题,无论如何,但你仍然有'.description'问题并尝试编辑'.desc'的答案。不会再匹配了。 – FelipeAls 2013-04-14 02:43:01
@FelipeAls我认为这是重点:/。通用是一件好事吗? – a7omiton 2013-04-14 04:23:04