1

所以我试图从多个表中获取单个项目数据,如 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 技巧可以用来实现数据结构,或者我需要修改模型中的结果数据?谢谢!

4

1 回答 1

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;
于 2018-07-13T01:13:22.213 回答