Reputation: 401
In my web application I want to print the no of records for a particular criteria . But the output I am getting is 0 , although there are many records for the matching condition. This code is inside a view of another model . "$data" belongs to that model
My code I implemented.
$temp=CHtml::encode($data->name);
$find=ConsumerRequest::model()->findAllByAttributes(array('requested_vegetable'=>$temp));
$count = count($find);
echo $count;
How should I resolve this?
Upvotes: 0
Views: 1480
Reputation: 1356
Try adding public $count
on your Model right after the class syntax. And use CDbCriteria to make a custome query.
Upvotes: 1
Reputation: 505
You can also do like this: $query='SELECT COUNT(id.tbl) FROM tbl';
CMIIW
Upvotes: 0
Reputation: 7556
I wouldn't use the findAll()
functions as this pulls over all the data for each row. You can simply use the count()
or countByAttributes()
function. I would guess your problem is probably this line:
$temp=CHtml::encode($data->name);
Most likely you are not storing it in the database HTML encoded. Try just doing this:
$count = ConsumerRequest::model()->countByAttributes(array('requested_vegetable'=>$data->name));
Upvotes: 1
Reputation: 1542
You can try these methods:
$temp=CHtml::encode($data->name);
$count = ConsumerRequest::model()->findAll(array("condition"=>"requested_vegetable = '$temp'"));
$count = count($find);
echo $count;
OR
$temp=CHtml::encode($data->name);
$Criteria = new CDbCriteria();
$Criteria->condition = "requested_vegetable = '$temp'";
$count = ConsumerRequest::model()->findAll($Criteria);
$count = count($find);
echo $count;
Upvotes: 0