codeigniter differentiate field names in JOINed tables

Posted by Patrick on Stack Overflow See other posts from Stack Overflow or by Patrick
Published on 2010-04-02T01:02:44Z Indexed on 2010/04/02 8:53 UTC
Read the original article Hit count: 188

Filed under:

Hi, I need to retrieve data from two tables. the first is a list of events, the second is a list of venues.

I have a fields with the same name in both tables: events.venue (which is an ID) and venues.venue is the name of the place, say "blues bar". The tables can be joined on events.venue = venues.id.

Snippet of my model:

$this->db->select('events.*, venues.*');
        $this->db->join('venues', 'events.venue = venues.id');
        if ($date != 'all')
        {
            $this->db->where('date', $date);
        }

        if ($keyword)
        {
            $this->db->like('description', $keyword);
            $this->db->or_like('band', $keyword);
            $this->db->or_like('venue', $keyword);
            $this->db->or_like('genre', $keyword);
        }

        $Q = $this->db->get('events');
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result() as $row)
            {
                $data[] = $row;
            }
        }

        $Q->free_result();
        return $data;

Snippet of the View:

foreach ($events as $row) {
            echo    "<p>{$row->band} ({$row->genre})<br />";
            echo    "Playing at: {$row->venue}<br /></p>"; // echoes "blues bar"
//more here...
}

2 Questions: 1) Why does $row->venue echo venues.venue, instead of events.venue?

B) how can I differentiate them? eg. What if I want to echo both events.venue and venues.venue? I can probably do something like "SELECT venues.venue as name_of_the_venue", but how can I do this when I've already selected *?

© Stack Overflow or respective owner

Related posts about codeigniter