Nam Hao
Nam Hao

Reputation: 67

Codeigniter: Pass variable to the View

I have to table article and table comment. In the home page I want to see how many comment for earch article.

Model

function show_latest_article($start,$display,$cate)
{
    $query=  $this->db->get_where('news_news', array('News_Cate_ID'=>$cate),$start,$display);
    if($cate==0)
    {
         $query=  $this->db->get_where('news_news',$start,$display);
    }

    return $query->result();
}
     function count_comment($id)
    {
        $query = $this->db->get_where('comment',array('comment_article_id'=>$id) );

         return $query->num_rows();   

    }

Controller

 function index() {
        $this->load->model('article');
        $data=array('article'=>$this->article->show_latest_article(0,8,1),
                    'sidebar'=>$this->article->side_bar(9),
                    'count_comment'=> $this->article->count_comment($id),

            ); 

        $this->load->view('page/index',$data);

    }

In the view I have this

    foreach($article as $art)
{
    echo $art->title."<br>";
    $id= $art->id;
    // I want to echo number of comment here.
   // Or I want to call function count_comment($id) 
}

Upvotes: 1

Views: 1154

Answers (6)

Iamzozo
Iamzozo

Reputation: 2358

It's not so clear, where the $id variable come from, but I suggest to join the comments table to your article. It's not a best practice to make queries within loops.

In your model:

public function show_latest_article($ids = array())
{
    return $this->db
        ->select('articles.*, COUNT(comments.id) as comment_count')
        ->where_in('articles.id', $ids) // You can skip this
        ->join('comments', 'comments.article_id = articles.id', 'left') 
        ->group_by('articles.id')
        ->get('articles')
        ->result();
}

In your controller:

public function index()
{
    $data['articles'] = $this->article->show_latest_article(array(0, 8, 1));
    $this->load->view('page/index', $data);
}

Just change the field name according to database columns. Also you can skip the where_in condition. (I'm not sure what the three number stands for).

Then in your view, you can simply access the field: $art->comment_count

EDIT: According to your comment:

$this->db
    ->select('a.*, (SELECT COUNT(*) FROM comment c WHERE c.comment_article_id = a.News_News_ID ) as counta')
    ->get('news_news')
    ->result();

Upvotes: 0

Mladen
Mladen

Reputation: 1

First of all in your controller you get count_comment only for specific article ID and not for all articles so you cant do foreach to display comment count of each article.

You need to setup you model better and in model function show_latest_article to use JOIN comments table and do count comment in query.

I will help you with query if you provide me more info about database table of article and comments.

SELECT 
    article.*,
    COUNT(comment.id),0) AS numberOfCommments

FROM article
LEFT JOIN comment
ON comment.article_id = article.id

GROUP BY article.id

Upvotes: 0

Bikash
Bikash

Reputation: 1938

    $this->load->model('article');
    $data['article'] = $this->article->show_latest_article(0, 8, 1);
    $data['sidebar'] => $this->article->side_bar(9);
    $data['count_comment'] => $this->article->count_comment($id);
    $this->load->view('page/index', $data);

And it should work.

Upvotes: 1

JiteshNK
JiteshNK

Reputation: 428

Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function. Here is an example using an array:

$data = array(
           'title' => 'My Title',
           'heading' => 'My Heading',
           'message' => 'My Message'
      );
$this->load->view('blogview', $data);

Also below is the example for object

$data = new Someclass();
$this->load->view('blogview', $data);

PLEASE NOTE: Note: If you use an object, the class variables will be turned into array elements. You can find out more from below ref URL

Ref: https://ellislab.com/codeigniter/user-guide/general/views.html

Upvotes: 0

Arizona2014
Arizona2014

Reputation: 1347

Controller :

 function index() {
    $this->load->model('article');
    $articles =  $this->article->show_latest_article(0,8,1);
    $count_comments = Array();
    for($i=0;$i<count($articles);$i++){
        $count_comments[$i] = $this->article->count_comment($articles->$id);
    } 
    $count_comments = 
    $data=array('article'=>$articles,
                'sidebar'=>$this->article->side_bar(9),
                'count_comment'=> $count_comments); 

    $this->load->view('page/index',$data);

 }

In the View :

$i=0;
foreach($article as $art)
{

    echo $art->title."<br>";
    $id= $art->id;
    echo $count_comment[$i]; 
    $i++;
}

Upvotes: 0

Vinie
Vinie

Reputation: 2993

I am writing this answer as you provided code. It could be more simpler if you give more database details. Try below code

function index() {
    $this->load->model('article');
    $articles = $this->article->show_latest_article(0,8,1);

    foreach($articles as $article)
    {
        $article->comment_count = $this->article->count_comment($article->id);
        $all_article[] = $article;
     }

     $data=array('article'=>$all_article,
                'sidebar'=>$this->article->side_bar(9)
        ); 

    $this->load->view('page/index',$data);

}

On view

foreach($article as $art)
{
    echo $art->title."<br>";
    $id= $art->id;
    echo $art->comment_count;

}

Upvotes: 0

Related Questions