neel
(Kazi Neel)
January 20, 2010, 3:14pm
1
I want to display total number of books per user
here is my admin controller
public function actionAdmin()
{
$this->processAdminCommand();
$criteria=new CDbCriteria;
$pages=new CPagination(Books::model()->count($criteria));
$pages->pageSize=self::PAGE_SIZE;
$pages->applyLimit($criteria);
$sort=new CSort('Books');
$sort->applyOrder($criteria);
$models=Books::model()->findAll($criteria);
$this->render('admin',array(
'models'=>$models,
'pages'=>$pages,
'sort'=>$sort,
));
}
funner
(Ujovlado)
January 20, 2010, 3:18pm
2
I want to display total number of books per user
here is my admin controller
public function actionAdmin()
{
$this->processAdminCommand();
$criteria=new CDbCriteria;
$pages=new CPagination(Books::model()->count($criteria));
$pages->pageSize=self::PAGE_SIZE;
$pages->applyLimit($criteria);
$sort=new CSort('Books');
$sort->applyOrder($criteria);
$models=Books::model()->findAll($criteria);
$this->render('admin',array(
'models'=>$models,
'pages'=>$pages,
'sort'=>$sort,
));
}
try this
http://www.yiiframework.com/doc/api/CActiveRecord#count-detail
funner
(Ujovlado)
January 20, 2010, 4:37pm
4
Neel:
do you have any example?
example:
if (Books::model()->count("is_active = 1") != 0) {
// do somethimg if count of active books not equals 0
}
else {
// echo "You haven't any books";
}
neel
(Kazi Neel)
January 21, 2010, 6:04pm
6
Count is working but it count all records. I want
if ($model->User_id == Yii::app()->user->id) then will count his books. can you tell me how
neel
(Kazi Neel)
January 21, 2010, 7:09pm
7
Here is my view code
<?php foreach($models as $n=>$model): ?>
<?php if ($model->User_id == Yii::app()->user->id){ ?>
<tr class="<?php echo $n%2?'even':'odd';?>">
<td><?php echo CHtml::encode($model->id); ?></td>
<td><?php echo CHtml::encode($model->name); ?></td>
<?php
if (TN::model()->count("Book_id = 1") != 0) { ?>
<td><?php echo CHtml::link(Book::model()->count("Book_id"), array('test','id'=>$model->id), array('target'=>'_blank')); ?></td>
<?php
}
else { ?>
<td><?php echo "No Books"; ?></td>
<?php } ?>
<td><?php echo CHtml::encode($model->Notes); ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
funner
(Ujovlado)
January 21, 2010, 7:46pm
8
can you tell me what would you like to do??
count is very simple.
[sql]
SELECT COUNT(*) FROM books
WHERE user_id
= 1;
[/sql]
is the same as
Books::model()->count("`user_id` = 1");
if you want to print table with user_id, user_name, and books_count, you should use a join, because counting books for every row is not very effective …
neel
(Kazi Neel)
January 21, 2010, 7:54pm
9
funner:
can you tell me what would you like to do??
count is very simple.
[sql]
SELECT COUNT(*) FROM books
WHERE user_id
= 1;
[/sql]
is the same as
Books::model()->count("`user_id` = 1");
if you want to print table with user_id, user_name, and books_count, you should use a join, because counting books for every row is not very effective …
My point is if ($model->User_id == Yii::app()->user->id) then count the book and print books number. I know the sql command and it count the books. but problem is when I use in yii then count all books.
Yes I want to print user_id, user_name and books_count.
neel
(Kazi Neel)
January 22, 2010, 2:59pm
10
Actually I want to count number of field records (books) where $model->User_id == Yii::app()->user->id). and view the total number of books