TiboK
TiboK

Reputation: 95

Foreach a model in codeigniter existing array

On my controller, i want to foreach another models to add the result in the existing array.

My controller:

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

Actualy when i @varp_dump my '$getConcerts_previous' on my view, I have:

  public 'cID' => string '45' (length=2)
  public 'cBand_ID' => string '8' (length=1)
  public 'cDate' => string '2018-06-22' (length=10)

And i want to know how add on this var dump for example

  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)

My database:

Concerts: cID | cName | cPlaceType | cPlaceID
Festivals: fID | fName
Halls: hID | hName

If cPlaceType = 1, select festival with cPlaceID, If cPlaceType = 2, select hall with cPlaceID,

Upvotes: 0

Views: 103

Answers (1)

Ryuk Lee
Ryuk Lee

Reputation: 744

Try this

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

In model

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

Upvotes: 1

Related Questions