CodeIgniter Forums

Full Version: Using the get_where active record class based on a previous set variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]mathew edison[/eluser]
Hey everyone,

First of let me tell you that I really enjoy using code igniter and that I really love the way it's formatted as it makes my life tons easier. I did however stumble into a problem which is more of the logical thinking nature than technical. I'm building my own portfolio for IT classes and I've added a blog. Now I have two Mysql tables one to hold the content of my posts and one for the comments. I related them by the field 'postid' in the comments table. Now on the same page I want to have a blog article and then number of comments posted on that article. How would I go at this? The following is what I have so far.

blog.php model:
Code:
function getposts()
    {
        $query = $this->db->get('blogarticles');
        return $query;
    }

welcome.php controller
Code:
$this->load->model('blog');
$data['queryblog'] = $this->blog->getposts();
$this->load->view('blog', $data);

blog.php view
Code:
<?php
if($queryblog->num_rows() > 0):
foreach ($queryblog->result() as $row):
?>
<div class="innerconttop"></div><div class="innercont">
<p><span class="newstitle">&lt;?php echo $row->title; ?&gt;</span>
<br>Posted by &lt;?php echo $row->user; ?&gt; on &lt;?php echo $row->date; ?&gt;<br><br>
&lt;?php echo $row->body ?&gt;
</div><div class="innercontbtm"><span class="comments">0 comments</span></div>
&lt;?php
endforeach;
endif;
echo "</p>";
if($queryblog->num_rows() == 0):
?&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;There are currently no articles posted. Our appologies</div>
&lt;?php
endif;
?&gt;

Any help on this matter would be highly appreciated and if you have any suggestions as to how I can improve this code please give them. I know the &nbsp;is a bad idea and I put that up as a temp. solution just untill I get some more time on my hands. Thanks in advance.

Sincerely
- Mathew