hi…
can anybody say how to display the total no of results in a variable (i.e. the variable has the result of a select query)
thanks…
hi…
can anybody say how to display the total no of results in a variable (i.e. the variable has the result of a select query)
thanks…
/* moved - not specific to Yii */
I advice you to visit http://php.net and study the PHP documentation.
BTW there is a count() function you can use.
Via relations would be like:
public function relations()
{
return array(
'postsCount'=>array(self::STAT, 'BlogPost', 'post_id'),
'commentsCount'=>array(self::STAT, 'BlogPostComment', 'post_id'),
'activeCommentsCount' => array(self::STAT, 'BlogPostComment', 'post_id', 'condition'=>'status=:st', 'params'=>array(':st'=>self::STATUS_ACTIVE)),
'tagsCount'=>array(self::STAT, 'BlogTag', '{{blog_tag_to_post}}(tag_id, post_id)'),
);
}
and in your controller, doing:
$post = BlogPost::model()->findByPk(1);
echo $post->postsCount;// will return all your posts
echo $post->commentsCount; // will return the number of comments for this post
echo $post->activeCommentsCount; // will return only active comments.
echo $post->tagsCount; // will return the number of tags this post have.
You can also do:
$posts=BlogPost::model()->findAll();
echo count($posts); // would give you an integer
//or
$postsCount=BlogPost::model()->count();
echo $postsCount;
Or by creating scopes and apply them, like:
//model
public function scopes()
{
return array(
'active'=>array(
'condition'=>'t.`status`=:st',
'params'=>array(':st'=>self::STATUS_ACTIVE),
),
'inactive'=>array(
'condition'=>'t.`status`=:st',
'params'=>array(':st'=>self::STATUS_INACTIVE),
),
);
}
//controller
$activePosts=BlogPost::model()->active()->findAll();
$inactivePosts=BlogPost::model()->inactive()->findAll();
echo count($activePosts);
echo count($inactivePosts);
//or
$activePostsCount=BlogPost::model()->active()->count();
$inactivePostsCount=BlogPost::model()->inactive()->count();
echo $activePostsCount;
echo $inactivePostsCount;
and probably many other ways…
@tri - thanks for moving the post while i was answering it.
/* moved back again - now Yii specific */
Sorry for that. Sometimes the user name doesn’t show up in italics while answering.
hi… thanks for ur reply… i get it done…