Foreach现有阵列中的codeigniter模型

Foreach现有阵列中的codeigniter模型,codeigniter,model-view-controller,foreach,model,Codeigniter,Model View Controller,Foreach,Model,在我的控制器上,我希望foreach其他模型将结果添加到现有阵列中 我的控制器: public function index() { $getConcerts_previous = $this->concert_model->getAllConcerts('previous'); foreach($getConcerts_previous as $concert) { $hallData = $this->hall_model->getH

在我的控制器上,我希望foreach其他模型将结果添加到现有阵列中

我的控制器:

public function index()
{
    $getConcerts_previous = $this->concert_model->getAllConcerts('previous');

    foreach($getConcerts_previous as $concert) {
        $hallData = $this->hall_model->getHallbyID($concert->cHall_ID);
        $concert->hHall_ID = isset($hallData) ? $hallData->hID : 'No data';
    }

    $this->render_layout('concerts/index', $this->data);
}
事实上,当我@varp_在我的视图中转储我的“$getConcerts_previous”时,我有:

  public 'cID' => string '45' (length=2)
  public 'cBand_ID' => string '8' (length=1)
  public 'cDate' => string '2018-06-22' (length=10)
我想知道如何添加这个var转储

  public 'cID' => string '45' (length=2)
  public 'cBand_ID' => string '8' (length=1)
  public 'cDate' => string '2018-06-22' (length=10)
  public 'hHall_ID' => string '1' (length=1)
我的数据库:

Concerts: cID | cName | cPlaceType | cPlaceID
Festivals: fID | fName
Halls: hID | hName
如果cPlaceType=1,选择带有cPlaceID的节日,如果cPlaceType=2,选择带有cPlaceID的大厅,

试试这个

public function index()
{
$this->data['getConcerts_next'] = $this->concert_model->getAllConcerts('next');

$getConcerts_previous = $this->concert_model->getAllConcerts('previous');

foreach($getConcerts_previous as $concert) {
  if($concert->cPlaceType == 1) {
    $hallData = $this->hall_model->getHallbyID($concert->cPlaceID);        
     //$concert->hHall_ID = isset($hallData) ? $hallData->hID : 'No data';  
     $concert->hallData = $hallData;  
     $concert->festivalData = NULL;
  } else {
    $festivalData = $this->festival_model->getFestivalbyID($concert->cPlaceID);     
     //$concert->hHall_ID =  isset($festivalData) ? $festivalData->fID : 'No data';           
     $concert->hallData = NULL;  
     $concert->festivalData = $festivalData;
  } 
}
var_dump($getConcerts_previous);
//include below line and you can pass data to view;
 $this->data['getConcerts_previous'] = $getConcerts_previous;
 $this->render_layout('concerts/index', $this->data);
}
模型中

public function getHallById($hallID) { 
  $this->db->where('hID', $hallID); 
  return $this->db->get($this->table)->row(); 
}

public function getFestivalById($hallID) { 
  $this->db->where('fID', $festivalID); 
  return $this->db->get($this->table)->row(); 
}

你想要什么还不清楚。你想把
hHall\u ID
添加到第一个var\u转储吗?@DFriend是的:)在我的foreach上,我有关于concert的所有信息(前缀c),现在我只需要关于hall的所有信息。你想把
$hallData
添加到
$getConcerts\u-previous
中吗?
concert\u-model->getAllConcerts(){..
@RyukLee是的,如果是的话“$concert->cHall_ID”是1,他选择ID为1的霍尔信息并将其添加到数组$getConcerts_之前谢谢你的回复(我编辑我的if-else^^)我实际上有这个错误:消息:试图获取非对象行号的属性:18(此行:$concert->hHall_ID=$hallData->hID;)我的查询是:公共函数getHallById($hallbyid){$this->db->where($hID',$hallID);$this->db->get($this->table);返回$this->db->get()->row()}显示
$this->festival\u model->getfestival-byid($concert->cPlaceID)
函数公共函数getfestival-byid($hallID){$this->db->where($fID',$festival-id);$this->db->get($this->table);返回$this->db->get()->row();}出现数据库错误错误代码:1096 Aucune table USRIée SELECT*文件名:C:/wamp64/www/concerts/system/Database/DB_driver.php行号:691我不理解您的查询?为什么您说
如果cPlaceType=1,选择cPlaceID为的节日,如果cPlaceType=2,选择cPlaceID为的大厅
。但是在函数
getFestival中ById
您放入条件
fID
,并在函数
getHallById
中放入条件
hID
。否则,您是否放入cPlaceID以运行2个查询?