Reputation: 55
I am new to yii framework.I have just create an app and I want to fetch data from DB based on some condition and then display that in views .For this I have tried few things in my controller :
if(isset($_GET['sku']))
{
$sku=$_GET['sku'];
$data=Products::model()->findAll("sku=$sku");
}
$this->render('checkout',array(
'data'=>$data,
));
and when in my views i try to print data as :
print_r($data);
I shows me a complex array , but i do not want that. The thing which i want is I can get an array from controllers which includes the data return by query based on some condition and in my views using foreach i can display them according to my need.
So please suggest me some good ways of fetching data from db and can display them in my views.
Thanks !!
Upvotes: 3
Views: 16783
Reputation: 11473
In your view use something like this:
<?php echo CHtml::encode($data->fieldname); ?>
Or better, use the Gii code generator to build your CRUD functions, and you will see several good examples for how to build these functions. Here is a link to a very good tutorial: http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app
Upvotes: 4
Reputation: 1464
Try to use DAO instead Active Record
$sql = "SELECT * FROM `products` WHERE sku = ".Yii::app()->request->getParam('sku', 0);
$data = Yii::app()->db
->createCommand($sql)
->queryAll();
$this->render('checkout',array(
'data'=>$data,
));
You should have db
component in config file(ex. config/main.php)
http://www.yiiframework.com/doc/guide/1.1/en/database.dao
Upvotes: 1