Welcome Guest, Not a member yet? Register   Sign In
Retrieving Comments
#1

[eluser]clintonbeattie[/eluser]
Hi,

I have part of my blog app complete but having problems using the id of my blog post to display all related comments.

This is what I have...

My Model
Code:
function getComments($id){
        $data = array();
        //selects one row matching that ID from the products table
        $options = array('post_id' => $id);
        $Q = $this->db->getwhere('comments',$options,1);
        if($Q->num_rows() > 0) {
            $data = $Q->row_array();
        }
        
        $Q->free_result();
        return $data;
    }

My Controller
Code:
function post($id)
    {
        //this simply allows you to link to the post page by using the id
        $post = $this->MBlog->getPost($id);
        if (!count($post)){
            redirect('welcome/index','refresh');
        }
        $data['post'] = $post;
        $data['page_title'] = $post['post_title'];
        $data['description'] = $post['description'];
        $data['keywords'] = $post['keywords'];
        $data['comments'] = $this->MBlog->getComments($id);
        $data['main'] = 'post';
        $this->load->vars($data);
        $this->load->view('template');
      }

My View
Code:
<?php
    echo "<h4>".$post['post_title']."</h4>";
    echo "<p>".date('d M Y', strtotime($post['post_date'])) ."</p>";
    echo "<p>".$post['post_intro']."</p>";
    echo "<p>".$post['post_text']."</p>";

?&gt;
<h4>Comments</h4>
&lt;?php
    echo "<p>".date('d M Y', strtotime($comments['comments_date'])) ."</p>";
    echo "<p>".$comments['comment_text']."</p>";
?&gt;

I have tried to loop through the comments and list them, but it seems to output random dates like January 1970. I don't know where it got this from.

When I print_r($comments); all I get is one entry...

Any help much appreciated.

Thanks,
C
#2

[eluser]vitoco[/eluser]
Hi , the error is in the method you use to retrieve de array from the result set
Code:
$data = array();
    //selects one row matching that ID from the products table
    $options = array('post_id' => $id);
    $Q = $this->db->getwhere('comments',$options,1);
    if($Q->num_rows() > 0) {
        $data = $Q->row_array(); <-----------------------------  result_array() INSTEAD of row_array()
    }
        
    $Q->free_result();
    return $data;

row_array get only the first row of the result set, even if you get many rows from the query , so the other go nowhere , instead of that , you must use result_array , that one , return a multidimentional array , containing all the rows that you get from the query

...and you can go throught those with

Code:
foreach( $data as $row )
    {
        // DO WHAT YOU DO WITH THE ROW
    }

Saludos
#3

[eluser]clintonbeattie[/eluser]
That's great. Thanks for that!!




Theme © iAndrew 2016 - Forum software by © MyBB