Retrive Data From Database

Hello

I am new in Yii.

Actually I try to retrieve data from database to print on htmlpage.

But one column of table is printing in actionIndex but i want to print whole table data.

I use for loop in action index but it printing whole index page multiple times.

This is the code:-

I siteContoller.php

public function actionIndex()


{   


            for ($i=1;$i<5;$i++)


            {


            $name=  Yiitest::model()->findByPk($i);


            $id=  Yiitest::model()->findByPk($i);


            $this->render('index', array('name'=>$name,'id'=>$id));


            }     


}

In index.php

<p>Your Name is :<?php echo $name->name?><br>

Your ID is:&lt;?php echo &#036;id-&gt;id?&gt;&lt;/p&gt;

Model is:-

<?php

class Yiitest extends CActiveRecord

{

public function tableName()


{


	return 'yiitest';


}


public function rules()


{





	return array(


		array('id, name', 'required'),


		array('id', 'numerical', 'integerOnly'=&gt;true),


		array('name', 'length', 'max'=&gt;50),





		array('id, name', 'safe', 'on'=&gt;'search'),


	);


}


public function relations()


{





	return array(


	);


}


public function attributeLabels()


{


	return array(


		'id' =&gt; 'ID',


		'name' =&gt; 'Name',


	);


}


public function search()


{





	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('id',&#036;this-&gt;id);


	&#036;criteria-&gt;compare('name',&#036;this-&gt;name,true);





	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


	));


}








public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}

}




public function actionIndex()

{

    $data = array();


    for ($i = 1; $i < 5; $i++) {

        $model = Yiitest::model()->findByPk($i);

        if ($model) {

            $data[] = array(

                'id' => $model->id,

                'name' => $model->name,

            );

        }

    }


    $this->render('index', array('data' => $data));

}



Or for all rows, dirty way (not recommended, use data provider methods instead)




public function actionIndex()

{

    $data = array();


    $models = Yiitest::model()->findAll();

    foreach ($model as $model) {

        $data[] = array(

            'id' => $model->id,

            'name' => $model->name,

        );

    }


    $this->render('index', array('data' => $data));

}



In index.php




<?php foreach ($data as $row): ?>

<p>Your Name is :<?php echo $row['name'] ?><br>

Your ID is: <?php echo $row['id'] ?></p>

<?php endforeach; ?>



Or for all rows directly with object, dirty way as well




public function actionIndex()

{

    $data = array();


    $models = Yiitest::model()->findAll();


    $this->render('index', array('models' => $models));

}



In index.php




<?php foreach (models as $model): ?>

<p>Your Name is :<?php echo $model->name ?><br>

Your ID is: <?php echo $model->id ?></p>

<?php endforeach; ?>