Codeigniter $查询->row_array() SQL $查询->result_array()
Codeigniter $query->row_array() SQL $query->result_array()
所以我试图从多个表中获取 单个 项目数据,如 itemID、itemSKU 等。并显示在视图中。
我在下面提供的示例项目有三张图片 attached.If 我使用 return $query->row_array();我只会得到一行 returned。如果我使用 $query->result_array(),我将得到三行 returned。对于单个项目页面,我不想使用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 技巧可以用来实现数据结构,或者我需要修改模型中的结果数据?
谢谢!
希望对您有所帮助:
您可以像这样修改结果数组:
$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;
所以我试图从多个表中获取 单个 项目数据,如 itemID、itemSKU 等。并显示在视图中。 我在下面提供的示例项目有三张图片 attached.If 我使用 return $query->row_array();我只会得到一行 returned。如果我使用 $query->result_array(),我将得到三行 returned。对于单个项目页面,我不想使用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 技巧可以用来实现数据结构,或者我需要修改模型中的结果数据? 谢谢!
希望对您有所帮助:
您可以像这样修改结果数组:
$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;