【问题标题】:Foreach a model in codeigniter existing array在codeigniter现有数组中foreach一个模型
【发布时间】:2018-05-16 20:44:09
【问题描述】:

在我的控制器上,我想为另一个模型添加结果到现有数组中。

我的控制器:

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_dump 我的“$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的大厅,

【问题讨论】:

  • 你想要什么不清楚。是不是要将hHall_ID添加到第一个var_dump?
  • @DFriend Yes :) 在我的 foreach 上,我有关于音乐会的所有信息(前缀 c),现在我只想要关于大厅的所有信息
  • 是否要将$hallData 添加到$getConcerts_previous 中?
  • concert_model->getAllConcerts(){..}的邮政编码
  • @RyukLee 是的,如果 "$concert->cHall_ID" 为 1,他选择 id 为 1 的 Hall 信息并添加到数组 $getConcerts_previous

标签: codeigniter model-view-controller foreach model


【解决方案1】:

试试这个

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(); 
}

【讨论】:

  • 感谢您的回复(我编辑我的 if else ^^)我实际上有这个错误:消息:尝试获取非对象行号的属性:18(此行:$concert->hHall_ID = $hallData->hID;) 我的查询是: public function getHallById($hallID) { $this->db->where('hID', $hallID); $this->db->get($this->table);返回 $this->db->get()->row(); }
  • 给我看$this->festival_model->getFestivalbyID($concert->cPlaceID)函数
  • 公共函数 getFestivalById($hallID) { $this->db->where('fID', $festivalID); $this->db->get($this->table);返回 $this->db->get()->row(); }
  • 发生数据库错误错误号:1096 Aucune table utilisée SELECT * 文件名:C:/wamp64/www/concerts/system/database/DB_driver.php 行号:691
  • 我不明白您的问题?你为什么说If cPlaceType = 1, select festival with cPlaceID, If cPlaceType = 2, select hall with cPlaceID。但是在函数getFestivalById 中,您输入了条件fID,而在函数getHallById 中,您输入了条件hID。在 if else 你把 cPlaceID 运行 2 个查询?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 2019-09-28
  • 2017-04-07
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多