Welcome Guest, Not a member yet? Register   Sign In
Need some assistance on making join statments
#1

[eluser]Unknown[/eluser]
Hello codeigniter forum!

I'm working on this project where I need to make some join statements.
I'm quite new to codeigniter and how it works with sql, so doing this have proven quite a challenge for me.

This is my current model, which I am goning to use for a "homemade" search function.
Code:
<?php
class Search_model extends CI_Model{
    
    public function __construct()
    {
        parent::__construct();
    }

    
    function search($searcharray)
    {    
    
        $this->db->select('user_id, user_name, email, status');
        $this->db->from('user');
        $this->db->like('user_name', $searcharray , 'both');    
        $this->db->or_like('email', $searcharray, 'both');
        $query = $this->db->get();    
        return $query->result();
    }
    
        
}

?>

As it is now, it just searches the user table for a match on either the email or user_name field.

This is the tricky part, I have these three more tables I need to add this.

A table named "forum_comment" with the fields "forum_comment_id, user_id, forum_comment_dt, comment_content" selected

A table named "picture" with the fields "picture_id, picture_title, picture_description" selected

and last a table named "topic" with the fields "topic_id, topic_title, topic_content, user_id" selected.

If you have any ideas, maybe a solution for me. I would be a very happy upcomming multimediadesigner.

- Best regards Todac.
#2

[eluser]vikascoollives[/eluser]
You can try by this way :

function test()
{
$sql = "SELECT tab1.fieldname, tab2.fieldname2,tab3.fieldname3 FROM table1 tab1 , table2 tab2 , table3 tab3 WHERE tab1.user_id = tab2.id AND tab2.id = tab3.uid ";

$this->db->query($sq);
$query = $this->db->get() ;
return $query->result();
}

// tab1.user_id = tab2.id AND tab2.id = tab3.uid is the required condition as per your application ;

Hope it will work for you .




Theme © iAndrew 2016 - Forum software by © MyBB