0

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,

10
  • What you want is not clear. Is it that you want to add hHall_ID to the first var_dump? Commented May 16, 2018 at 21:04
  • @DFriend Yes :) On my foreach, i have all infos about concert (prefix c), now i juste want all infos about hall Commented May 16, 2018 at 21:07
  • Do you want to add $hallData into $getConcerts_previous ? Commented May 17, 2018 at 2:17
  • Post code for concert_model->getAllConcerts(){..} Commented May 17, 2018 at 3:01
  • @RyukLee Yes, if "$concert->cHall_ID" is 1, he select the Hall infos with id 1 and add to the array $getConcerts_previous Commented May 17, 2018 at 7:08

1 Answer 1

1

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(); 
}
Sign up to request clarification or add additional context in comments.

15 Comments

Thanks for your reply (i edit my if else ^^) I have this error actually: Message: Trying to get property of non-object Line Number: 18 (This line: $concert->hHall_ID = $hallData->hID;) My query is: public function getHallById($hallID) { $this->db->where('hID', $hallID); $this->db->get($this->table); return $this->db->get()->row(); }
show me $this->festival_model->getFestivalbyID($concert->cPlaceID) function
public function getFestivalById($hallID) { $this->db->where('fID', $festivalID); $this->db->get($this->table); return $this->db->get()->row(); }
A Database Error Occurred Error Number: 1096 Aucune table utilisée SELECT * Filename: C:/wamp64/www/concerts/system/database/DB_driver.php Line Number: 691
I don't understand your query? Why did you say If cPlaceType = 1, select festival with cPlaceID, If cPlaceType = 2, select hall with cPlaceID. But in function getFestivalById you put condition fID and in function getHallById you put condition hID. In if else you put cPlaceID to run 2 queries ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.