bob
bob

Reputation: 445

Table join: unable to show the data what I want

If I have table like this:

ID | Title | Topic      | Summary    
1   |   A   | Technology | ...    
2  |   B   | Health     | ...    
3  |   C   | Sport      | ...

This is my CI_Model:

function show($limit, $offset)
    {
        $this->db->select('document.id, document.title, document.summary, document.id_topic AS topic');
        $this->db->from('document');
        $this->db->join('topic', 'topic.id_topic = document.id_topic');
        $this->db->limit($limit, $offset);
        $this->db->order_by('id', 'asc');
        return $this->db->get()->result();
    }

This is my Controller:

            $docdata = $this->Trainingmodel->show($this->limit, $offset);
        ...
        $this->table->set_heading('ID', 'Title', 'Topic', 'Summary');
        foreach ($docdata as $doc)
        {
            $this->table->add_row($doc->id, $doc->title, $doc->topic, $doc->summary);                                                                                               
        }

Evidently the topic shows it's id, not name. For example:

ID | Title | Topic      | Summary    
1   |   A   | 1 | ...    
2  |   B   | 2  | ...    
3  |   C   | 3  | ...

What should I do? I want to show topic's name, not topic's id.

Upvotes: 0

Views: 52

Answers (3)

xdazz
xdazz

Reputation: 160833

$this->db->select('document.id, document.title, document.summary, topic.topic');
$this->db->from('document');
$this->db->join('topic', 'topic.id = document.id_topic');

Upvotes: 1

Sean
Sean

Reputation: 12433

Looking at your table structure you posted as comments in the other answers, I think you need topic.topic in your select() and topic.id = document.id_topic in your join() -

$this->db->select('document.id, document.title, document.summary, topic.topic');
$this->db->from('document');
$this->db->join('topic', 'topic.id = document.id_topic');

Upvotes: 1

Þaw
Þaw

Reputation: 2057

Maybe its because of this document.id_topic

$this->db->select('document.id, document.title, document.summary, document.id_topic AS topic');

should it be something like document.topic or document.name_topic ?

Upvotes: 0

Related Questions