Welcome Guest, Not a member yet? Register   Sign In
Multiple Joins
#1

[eluser]-Gareth[/eluser]
Hello,

I am new to CI and the MVC pattern in general, so please forgive me if this is a really stupid question!

I am making, as an introductory application/ guide into CI a forum. In my forum controller I have this:

Code:
$this->db->select('
                            categories.id as category_id,
                            categories.title as category_title,
                            topics.id as topic_id,
                            topics.title as topic_title,
                            topics.author_id as topic_author
                          ');
        
        $this->db->from('categories');
        $this->db->join('topics', 'topics.cat_id = categories.id', 'LEFT');
        
        $query = $this->db->get();
        
        foreach($query->result() as $category) {
            $cat[$category->category_id]["title"] = $category->category_title;
            $cat[$category->category_id]["topics"][] = array(
                                                                "id" => $category->topic_id,
                                                                "title" => $category->topic_title,
                                                                "author" => $category->topic_author
                                                             );
        }
        
        $data['cat'] = $cat;
        $this->load->view('forumIndex', $data);
    

        }


And then in my view:

Code:
<div id="categories">    
            
        &lt;? foreach($cat as $category_id => $category): ?&gt;
        
            <div class="category_title">
            
                <h3>
                    &lt;?=anchor("categories/view/".$category_id, $category["title"]);?&gt;
                </h3>
                
            </div>
            
            &lt;? foreach($category['topics'] as $topic): ?&gt;
                &lt;?=$topic["title"]; ?&gt; by &lt;?=$topic["author"]; ?&gt;<br />
            &lt;? endforeach; ?&gt;
            
    &lt;? endforeach; ?&gt;

I have got the one join working, but is there any way I can have a second? I would like to store the author details in a seperate table, using author_id as a link between the two.

I am not sure on joins in a normal mysql: I have always found a way to not use them, but now I have to use them I cant work them out!

Many thanks.
#2

[eluser]srisa[/eluser]
As per the manual you can have more than one $this->db->join().
#3

[eluser]-Gareth[/eluser]
Sorry, misread that completely!

I seem to be getting an error if i try this, though. I deleted it at the time so I'll retry and post the code up if I get the same error.




Theme © iAndrew 2016 - Forum software by © MyBB