因此,我试图从多个表中获取单个项目数据,例如itemID,itemSKU等。并在视图中显示。 我在下面提供的示例项带有三个图像。如果使用return $ query-> row_array();我只会返回一行。如果我使用$ query-> result_array(),我将返回三行。对于单项页面,我不想使用foreach来显示结果。
我的模型
$this->db->join('itemsToCats', 'items.itemID = itemsToCats.itemId','left');
$this->db->join('categories', 'itemsToCats.catId = categories.ctgID', 'left');
$this->db->join('itemImg', ' items.itemID = itemImg.itemID', 'left');
$this->db->select('items.itemID, itemSKU, itemName, itemSDesc, addDate, chgDate, ctgID, ctgName, parentID, itemImg');
$query = $this->db->get_where('items', array('items.itemID' => $itemID));
return $query->row_array();
输出
Array
(
[itemID] => 521
[itemSKU] => image part number
[itemName] => multiple images
[itemSDesc] =>
[addDate] => 2018-07-12 16:17:09
[chgDate] => 0000-00-00 00:00:00
[ctgID] => 67
[ctgName] => Pipe Bending
[parentID] => 46
[itemImg] => 234-gy_hello1.png
)
如果我使用return $ query-> result_array() 我明白了:
Array
(
[0] => Array
(
[itemID] => 521
[itemSKU] => image part number
[itemName] => multiple images
[itemSDesc] =>
[addDate] => 2018-07-12 16:17:09
[chgDate] => 0000-00-00 00:00:00
[ctgID] => 67
[ctgName] => Pipe Bending
[parentID] => 46
[itemImg] => 234-gy_hello1.png
)
[1] => Array
(
[itemID] => 521
[itemSKU] => image part number
[itemName] => multiple images
[itemSDesc] =>
[addDate] => 2018-07-12 16:17:09
[chgDate] => 0000-00-00 00:00:00
[ctgID] => 67
[ctgName] => Pipe Bending
[parentID] => 46
[itemImg] => cac1f0ad0720ac05e76fd990de2d309e.png
)
[2] => Array
(
[itemID] => 521
[itemSKU] => image part number
[itemName] => multiple images
[itemSDesc] =>
[addDate] => 2018-07-12 16:17:09
[chgDate] => 0000-00-00 00:00:00
[ctgID] => 67
[ctgName] => Pipe Bending
[parentID] => 46
[itemImg] => eee779a15e340e2a0f4d0b682e900862.png
)
)
这是我想要的:
Array
(
[itemID] => 521
[itemSKU] => image part number
[itemName] => multiple images
[itemSDesc] =>
[addDate] => 2018-07-12 16:17:09
[chgDate] => 0000-00-00 00:00:00
[ctgID] => 67
[ctgName] => Pipe Bending
[parentID] => 46
[itemImg] => Array
(
[0] => 234-gy_hello1.png
[1] => cac1f0ad0720ac05e76fd990de2d309e.png
[2] => eee779a15e340e2a0f4d0b682e900862.png
)
)
是否可以使用一些SQL技巧来实现数据结构,或者需要修改模型中的结果数据? 谢谢!
答案 0 :(得分:2)
希望这对您有帮助:
您可以像这样修改结果数组:
$this->db->join('itemsToCats', 'items.itemID = itemsToCats.itemId','left');
$this->db->join('categories', 'itemsToCats.catId = categories.ctgID', 'left');
$this->db->join('itemImg', ' items.itemID = itemImg.itemID', 'left');
$this->db->select('items.itemID, itemSKU, itemName, itemSDesc, addDate, chgDate, ctgID, ctgName, parentID, itemImg');
$query = $this->db->get_where('items', array('items.itemID' => $itemID));
if ($query->num_rows() > 0 )
{
foreach ($query->result_array() as $key => $item)
{
$data['itemID'] = $item['itemID'];
$data['itemSKU'] = $item['itemSKU'];
$data['itemName'] = $item['itemName'];
$data['itemSDesc'] = $item['itemSDesc'];
$data['addDate'] = $item['addDate'];
$data['chgDate'] = $item['chgDate'];
$data['ctgID'] = $item['ctgID'];
$data['ctgName'] = $item['ctgName'];
$data['parentID'] = $item['parentID'];
$data['itemImg'][$key] = $item['itemImg'];
}
}
//print_r($data);die;
return $data;