codeigniter - list related data

0

Hello,

I'm trying to show the name of a channel associated with an event through an FK, but I'm not able to show it.

Model:

public function get_events() {
$this->db->get('events');
$this->db->join('channels', 'events.channel_id = channels.id');
$this->db->join('colors', 'channels.color_id = colors.id');
return $query = $this->db->get('events')->result();}

View:

foreach($events as $event){  
echo '<tr class="odd gradeX">';
    echo "<td>".$event->start." - ".$event->end."</td>";
    echo '<td class="center">'.$event->channels->name.'</td>';
    echo '<td class="center">'.$event->status_id.'</td>';
echo "</tr>";}

Any suggestions?

    
asked by anonymous 19.11.2015 / 13:38

2 answers

0

Good afternoon. I would change to the following code:

public function get_events() {
$this->db->Select('events.*, channels.*, colors.*');
$this->db->join('channels', 'events.channel_id = channels.id');
$this->db->join('colors', 'channels.color_id = colors.id');
$this->db->from('events');
$res = $this->db->get()->result();
return $res; 
}

In this way, you have access to the data of all the tables. Another advice I give you is how you do not specify differences between ids, work with apelidos such as: event.id as idevents or channels.id as idchannels

I hope I have helped.

Oss

    
23.12.2015 / 19:37
0

Do this:

Model

public function get_events() {
 $this->db->select('*');
 $this->db->from('events');
 $this->db->join('channels', 'events.channel_id = channels.id');
 $this->db->join('colors', 'channels.color_id = colors.id');
 return $this->db->get()->result();
 }

Controller

public function pagina() {
    $this->load->model('evento_model', 'evento');
    $dados['events'] = $this->evento->get_events();
    $this->load->view("suapaginadeview.php", $dados);
}

View

foreach($events as $event){  
 echo '<tr class="odd gradeX">';
 echo "<td>".$event->start." - ".$event->end."</td>";
 echo '<td class="center">'.$event->channelname.'</td>';
 echo '<td class="center">'.$event->status_id.'</td>';
 echo "</tr>";
 }

In this way, you have access to all data in the query tables. (If you have any table fields with the same name you will have to use nicknames in the query.)

    
16.07.2018 / 14:02