Welcome Guest, Not a member yet? Register   Sign In
[SORTED] getting data from 2 different table according to matching id. totally confused :/ How to get data from 2 differ
#1

[eluser]artlover[/eluser]
I have a model (below), it returns last 9 works from works table.

and I need to get data from a different table (tbl_photo) which has the id_work.

normally I would run a sql in a while loop and to match tbl_photo.id_work with table work.id

but how can I do that in codeigniter and how can pass that in MVC :/ im very confused. please help!! thanks!

Code:
function get_last9work()
    {
        $q = $this->db->get('works', 9);
    
        if($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        } else {
            return false;
        }
    }
#2

[eluser]artlover[/eluser]
Code:
$this->db->select('works_image.*', 'works.*');
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();

foreach ($result->result() as $row) {
    echo " # " . $row->id . " - " . $row->thumb . " - " . $row->wname . "<br />";
}

I made something like that. it looks like workng fine. but how can I get data from different tables. I need wname data from works table and photoname data from works_image table.
#3

[eluser]artlover[/eluser]
sorted Smile thanks anyway.

Code:
$this->db->select('works_image.*, works.*');
        $this->db->where('works_image.default_img', 1);
        $this->db->from('works_image', 'works');
        $this->db->join('works', 'works.id = works_image.id_work');
        $result = $this->db->get();




Theme © iAndrew 2016 - Forum software by © MyBB