0

i want to count comments at every article ..but the comments counter place between foreach loop..so i cant count comments correctly...so i want loop but i dont need loop in the counter

articles_controller.php

$count = $this->Article->Comment->find(
    'count', array('conditions' =>  array('Comment.status' => 1))
);

articles/index.ctp

<?php
// initialise a counter for striping the table
$count = 0;

// loop through and display format
foreach($articles as $article):
    // stripes the table by adding a class to every other row
    $class = ( ($count % 2) ? " class='altrow'": '' );
    // increment count
    $count++;

?>

<?php 
    echo $html->link(
        $article['Article']['title'], 
        array( 'action' => 'view', $article['Article']['id'])
    );  
?>

<!-- date and comment counter -->
<p class="entry-meta">
    <span class="date"><?php echo $article['Article']['created']; ?></span> <span class="meta-sep">|</span>
    <span class="comments-link">
    <!-- here i will put the comment counter -->
    <a href="declining-health.html#respond"> <?php  echo $count ['Comment'];>Comments</a>
    </span>
</p>

<?php endforeach; ?>

3 Answers 3

2

Use the counterCache property in the Post->Comment relationship and make sure you have a comment_count field in the post model.

IE:

<?php
    class Post extends AppModel {
        public $name = 'Post';
        public $hasMany = array(
            'Comment' => array(
                'className'    => 'Comment',
                'foreignKey'   => 'post_id'
            )
        );
    }
?>

<?php
    class Comment extends AppModel {
        public $name = 'Comment';
        public $belongsTo = array(
            'Post' => array(
                'className'    => 'Comment',
                'counterCache'   => true
            )
        );
    }
?>
// in the database table for table posts make sure to do the following

add a column: comment_count int(11) default 0

Now when you add a new post, you will have a comment_count field with an initial value of 0. When a new comment is added or removed from a Post the count value will automagically update.

That allows you to simply loop over the posts array and echo the value of the comment_count field or use the value to check for comments then embed an element etc.

Sign up to request clarification or add additional context in comments.

Comments

2

You can count the number of items in an array with sizeof().

5 Comments

you mean id do this <?php echo sizeof($count ['Comment']) ;>
i tjink you dont understand me ... the count i mean not function but mysql code like this SELECT COUNT
What is the problem then? You can do SELECT COUNT(*) FROM tbl to get the number of rows in the table tbl.
ok but the result between foreach loop.. i dont need to loop this result
You can do sizeof($arr) without first having looped through $arr.
0

Krisitian gave you the answer, but may I suggest you do all your styling in the frontend.

css pseudo selector

//styles every second(even) elements
.your_class:nth-child(2n){background-color:hotpink;}

tr:nth-child(2n){background-color:hotpink;}

...these also work with jquery

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.