[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">
<? foreach($cat as $category_id => $category): ?>
<div class="category_title">
<h3>
<?=anchor("categories/view/".$category_id, $category["title"]);?>
</h3>
</div>
<? foreach($category['topics'] as $topic): ?>
<?=$topic["title"]; ?> by <?=$topic["author"]; ?><br />
<? endforeach; ?>
<? endforeach; ?>
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.