简体   繁体   中英

Codeigniter & Datamapper: Count() comments

I am having trouble counting how many comments an article has. It is one to many relationship and I have a column in the comments table called (article_id) and on my index page where are displayed all the articles I want to get how many comments each one has.

I am using DataMapper . What this code gives me if I have 2 Articles the oldest one has 7 comments and the new one has 2 comments then it will display the noOfComments of the new one on both articles. So Instead of being Comments:2 Comments:7 is Comments: 2 and again Comments: 2 Any Ideas ?

$articles = new Article_model();
$comments = new Comment_model();

$articles->order_by('pubdate', 'desc')->get();
$id = $articles->id;

$noOfComments = $comments->where('article_id', $id)->count();

$recent_articles = array();

foreach ($articles as $article)
{
  $single_article = array(
            'id' => $article->id,
            'title' => $article->title,
            'pubdate' => $article->pubdate,
            'text' => $content,
            'cover_img' => $article->cover_img,
            'noOfComments' => $noOfComments,
            );
   array_push($recent_articles, $single_article);
 }

Ok let's have a look over the code:

// These are the datamaper objects
$articles = new Article_model();
$comments = new Comment_model();

// Get all of the articles ordered by pubdate and sorted desc
$articles->order_by('pubdate', 'desc')->get();
// $id now contains the articles
$id = $articles->id;

// Count the article id's for each comment that is associated with the article_id
$noOfComments = $comments->where('article_id', $id)->count();

// Create an array to store data
$recent_articles = array();
// Loop over the articles and create an array of information
foreach ($articles as $article)
{
  $single_article = array(
            'id' => $article->id,
            'title' => $article->title,
            'pubdate' => $article->pubdate,
            'text' => $content,
            'cover_img' => $article->cover_img,
            'noOfComments' => $noOfComments,
            );
   // push the data to $recent_articles
   array_push($recent_articles, $single_article);
 }

So in your view you are using $single_article and $recent_articles, and your passing $noOfComments which would be identical for both of the array's which is why you are getting the output you have.

EDIT:

$noOfComments = $comments->where('article_id', $id)->count(); All I see is your passing the article id into this $comments->where() function which is returning the count of the total number of comments. That is all it's doing, it isn't distinguishing between old or new.

How is it suppose to distinguish between old and new is the question, what does $comments->where() function look like? Can you extend it to accept multiple id's(an array of them) so you can than compare them in the function and return both counts(in an array) to add to your array?

Assuming Comments is related with Articles with a many-to-one relationship (many comments for one article):

$articles = new Article_model(); // IS YOUR TABLE REALLY NAMED 'article_models' ?
$comments = new Comment_model(); // IS YOUR TABLE REALLY NAMED 'comment_models' ?

$articles->order_by('pubdate', 'desc')->get_iterated();
$nr_of_articles = $articles->result_count();

if (count($nr_of_articles) > 0)
{
    foreach ($articles as $article) 
    {
        $recent_articles[] = $article->to_array();

        // OR, IF LIKE ME YOU PREFER ID'S AS KEYS
        $article_id = $article->id;
        $recent_articles[$article_id] = $article->to_array();

        $related_comments = $article->comment_model->all_to_array(); // ASSUMING YOUR TABLE IS NAMED 'comment_models'
        $recent_articles[$article_id]['related_comments'] = $related_comments;
    }
    unset($articles); // REMEMBER TO FREE YOUR SERVER'S RAM! DATAMAPPER OBJECTS ARE HEAVY
}


if for some reason

$related_comments = $article->comment_model->all_to_array();

doesn't work, try:

        $related_comments = $article->comment_model->get_iterated(); // ASSUMING YOUR TABLE IS NAMED 'comment_models'
        $nr_of_related_comments = $related_comments->result_count();

        if (count($nr_of_related_comments) > 0)
        {
            foreach ($related_comments as $related_comment) 
            {
                $comment_id = $related_comment->id;
                $recent_articles[$article_id]['related_comments'][$comment_id] = $related_comment->to_array();
            }
            unset($related_comments);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM