CGridView display data from database in Yii PHP Framework
Step 1: Create a php project using Yii Framework.(Example: 'studentdetails')Step 2: Create a database named 'sample' in Mysql or use the existing database.
Step 3: Create a table named as 'studentdetails' in sample DB and Insert the following queries.
CREATE TABLE `studentdetails` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`age` INT(11) NOT NULL,
`mark1` INT(11) NOT NULL,
`mark2` INT(11) NOT NULL,
`mark3` INT(11) NOT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `studentdetails` (`name`, `age`, `mark1`, `mark2`, `mark3`) VALUES
('Student1', 14, 68, 85, 68),
('Student2', 15, 89, 89, 85),
('Student3', 14, 89, 89, 85),
('Student4', 15, 89, 89, 85);
Step 4: Open your config page in the following directory D:\xampp\htdocs\studentdetails\protected\config\main.php and edit the file as follows.
'db' => array(
'connectionString' => 'mysql:host=localhost;dbname=sample',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
Step 5: Create a model file in the name 'studentForm.php' in the following directory D:\xampp\htdocs\studentdetails\protected\models\studentForm.php and the content as follows.
<?php class studentForm extends CFormModel { public $name; public $age; public $mark1; public $mark2; public $mark3; public function getDisplayData() { $sql = "select * from studentdetails"; $rawdata = Yii::app()->db->createCommand($sql)->queryAll(); $data = new CArrayDataProvider($rawdata, array( 'keyField' => 'id', 'sort' => array(//optional and sortring 'attributes' => array( 'id', 'name', 'age', 'mark1', 'mark2', 'mark3'), ), 'pagination' => array('pageSize' => 10)) ); return $data; } } ?>
Step 6: Create view file named as 'student.php' in the following directory D:\xampp\htdocs\studentdetails\protected\views\site\student.php and the content as follows.
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'student-grid',
'dataProvider' => $model->getDisplayData(),
//'filter' => $model,
'selectableRows' => 2,
'columns' => array(
array(
'header' => 'S.No',
'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)'
),
array(
'name' => 'id',
'header' => 'ID'
),
array(
'name' => 'name',
'header' => 'Student Name'
),
array(
'name' => 'age',
'header' => 'Age'
),
array(
'name' => 'mark1',
'header' => 'Mark1'
),
array(
'name' => 'mark2',
'header' => 'Mark2'
),
array(
'name' => 'mark3',
'header' => 'Mark3'
)
),
));
?>
Click here for CGridView Documentation
Step 7: Add a new action in the siteController class in the following directory D:\xampp\htdocs\studentdetails\protected\controllers\SiteController.php
public function actionStudent() {
$model = new studentForm;
$this->render('student', array('model' => $model));
}
Step 8: Add a new menu in the layout by editing the following file 'main.php' in the following directory D:\xampp\htdocs\studentdetails\protected\views\layouts\main.php
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'Student Details', 'url'=>array('/site/student')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
)); ?>
Step 9: Run the application and see the result.
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeletePHP 7 Training in chennai | PHP 7 Training Course
PHP 7 Training in chennai | Online PHP 7 Course
Resources like the one you mentioned here will be very useful to me ! I will post a link to this page on my blog. I am sure my visitors will find that very useful
ReplyDeleteMicrosoft azure training in Bangalore
Power bi training in Chennai
Really nice experience you have. Thank you for sharing. It will surely be an experience to someone.
ReplyDeletepython training in chennai
Python Online training in usa
python course institute in chennai
I appreciate that you produced this wonderful article to help us get more knowledge about this topic. I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!
ReplyDeletepython training in chennai
Python Online training in usa
python course institute in chennai
This comment has been removed by the author.
ReplyDeleteResources like the one you mentioned here will be very useful to me ! I will post a link to this page on my blog. I am sure my visitors will find that very useful
ReplyDeletesap training in chennai
sap training in omr
azure training in chennai
azure training in omr
cyber security course in chennai
cyber security course in omr
ethical hacking course in chennai
ethical hacking course in omr
ReplyDeleteWonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
Offshore PHP Programming in India