Latest news:

Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now

Yii load data into select box from database

Yii load data into select box from database

In this article we will learn how to load data into yii select box from database.

We will follow below steps

  • Create a function in model class
  • Call that function to view page to get the data

Lets say we have a table as pos_mac, now we will add a method named as getMacList() in POS_MAC model class.

This method will load the all table data of pos_mac. Yii CHtml::listData function convert this data into array list

For Example

public function getMacList() {
        $model = POS_MAC::model()->findAll();
        $data = CHtml::listData($model, 'MID', 'MAC_ADDRESS');
        return $data;
Now in the view page we need to call that method.
For example
echo $form->dropDownList($model, 'POS_MAC_MID',$model->getMacList(),array('class'=>'form-control'));
Yii has built-in helper function dropDownList which accept following parameters
  • Model object
  • ID key/Attribute Name
  • Array List of Table data
  • Options eg (class,id,.....)
Tags : PHP, Yii,

Views : 761

Subscribe Us

Follow Us