Easy Question - Comments_View and Showing Article

#1
[eluser]Kesey[/eluser]
I'm a beginner w/ PHP and CI. I've followed the blog tutorial and I'm trying to get the entry that one is commenting on to show up at the top of the comments page.

I can get all of the entries to show up, similar to how it works on the main blog page, but how can I get just the one entry instead of using the loop to pull them all?

Controller:
Code:
function comments()
    {
        $data['query'] = $this->db->get('entries');
        $this->load->view('commentsview', $data);
    }

View:
Code:
<!--Get Article-->
    <?php foreach($query->result() as $row): ?>
        <h3>&lt;?=$row->title?&gt;</h3>
        <p>&lt;?=$row->body?&gt;</p>
        <p>&lt;?=anchor('blog', 'Back to Blog');?&gt;</p>
        <hr>        
    &lt;?php endforeach; ?&gt;
&lt;!-- End Get Article--&gt;

Thanks!

#2
[eluser]louis w[/eluser]
Just add a limit to your get. This will limit the number of rows returned.

Code:
$data['query'] = $this->db->get('entries', 1);

#3
[eluser]gtech[/eluser]
Controller:
Code:
function comments()
    {
        $query = $this->db->get('entries');
        $data['query'] = $query->result_array();
        $this->load->view('commentsview', $data);
    }

View:
Code:
<h3>&lt;?=$query[0]['title']?&gt;</h3>


there are many other ways to do the same thing though.

#4
[eluser]Kesey[/eluser]
louis, gtech - thanks for the responses.

I tried both of your suggestions and both of these solutions only display the first entry regardless of which entry the comments belong to. Is there a way to pass they entry_id so that the entry corresponding to the comments is displayed?

Thanks.

#5
[eluser]Pascal Kriete[/eluser]
You can add the id to the end of your url:

example.com/blog/comments/10

And it will then be passed into your controller function as an argument.

Code:
function comments($id)
{
    $query = $this->db->get_where('entries', array('id' => $id), 1, 0);
    $data['query'] = $query->row_array();
}

#6
[eluser]gtech[/eluser]
Code:
function comments($entryid)
    {
        $query = $this->db->getWhere('entries',array('Entry_ID'=>$entryid));
        $data['query'] = $query->result_array();
        $this->load->view('commentsview', $data);
    }

View:
Code:
<h3>&lt;?=$query[0]['title']?&gt;</h3>

your url would pass in the entryid... <siteurl>/index.php/<controller>/comments/<entry_id>

#7
[eluser]gtech[/eluser]
inparos solution is better as if you use row array it only returns 1 row.

in the view you would access it by

Code:
<h3>&lt;?=$query['title']?&gt;</h3>

#8
[eluser]Kesey[/eluser]
inparo - what would the view look like for that?

I have:
Code:
<h3>&lt;?=$query[0]['title']?&gt;</h3>

I've tried:
Code:
<h3>&lt;?=$query['id']['title']?&gt;</h3>

The latter returns the Post Id, now to get the title that corresponds to that post id?

Thanks.

#9
[eluser]gtech[/eluser]
kessey - I already answered the question I think.

<h3>&lt;?=$query['title']?&gt;</h3>

result_array can return more than one row if the table allows duplicate entries so you need $query[<number>]['title']

row_array only returns one entry.. if there is a duplicate entry it will return the last one returned so you only need $query['title'] to access the data.

#10
[eluser]Kesey[/eluser]
I got it, but I'm sure there's a better way to get the text on the same line?
Code:
<h3>&lt;?=$query['id']?&gt; &lt;?=$query['title']?&gt; &lt;?=$query['body']?&gt;</h3>


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.