Good morning all my dear friends in apervious lesson we discussed about the CRDU(Lesson no 10) in a quick manner with the help of Yii web devoloping tool called Gii..
Now My aim is to do it with hand written codes..
First I am going to retrive data from a database.
Step 01.Create a database called 'data' and a table called infos.
Add three fields called Id,Name,Age in to it.
Please these are case sensitive.So be careful.
Add some data into the table.
Now your database,table should be like below.
Now My aim is to do it with hand written codes..
First I am going to retrive data from a database.
Step 01.Create a database called 'data' and a table called infos.
Add three fields called Id,Name,Age in to it.
Please these are case sensitive.So be careful.
Add some data into the table.
Now your database,table should be like below.
Step 02:
Now you need to configure the database in yii...
So open main.php in \protected\config directory and find below
/*
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=testdrive',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
*/
Then uncomment it and edit it as below
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=data',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
Now basic configaration is ok..
Step 03:
Now what you need is to create model,view,controller.
First I will create the controller.Below is my controller.Save it as mycontroller.php in protected\controllers directory.
<?php
class mycontroller extends Controller{
public function actionIndex() {
$dataProvider=new CActiveDataProvider('myout');//here my out is the name of model(later we will create it.).
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
}
?>
Step 04 : Now I will create my model.Save the below codes in \protected\models directory as myout.php
<?php
class myout extends CActiveRecord
{
public function tableName()
{
return 'infos';
}
}
?>
Step 05: Now I need my view.Here I use two views.
Now create a folder called 'my' in protected\views\ directory.
First type below codes and save it as index.php in protected\views\my directory.
<?php
$this->breadcrumbs=array(
'infos',
);
?>
<h1>Posts</h1>
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
Step 06: Now I need another view to print/echo data what we retrive.So save the below codes in protected\views\my
directory as _view.php
<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::encode($data->Id); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>
<?php echo CHtml::encode($data->Name); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('age')); ?>:</b>
<?php echo CHtml::encode($data->Age); ?>
<br />
</br>
Step 07 : That is all...Now Go to the webbrowser and type http://localhost/yii/myyii/index.php?r=my/index
And you will have retrive the data from tha database as follows.
That is all for today..bye..
Do not forget to have a hard drinks!!!!!!!!!!! :)
Do not forget to have a hard drinks!!!!!!!!!!! :)