Hitesh Modha
Hitesh Modha

Reputation: 2790

DropDownList from database in YII

i am creating yii application first time, i have used following code to create dropdownlist in my view

echo $form->dropDownList($model,'CodeLookupId',Forms::model()->findAll());

it show me error

include(Forms.php): failed to open stream: No such file or directory

C:\wamp\www\LearningYii\protected\views\codelookup_form.php(35):

     <?php echo $form->textField($model,'ShortDesc',array('size'=>50,'maxlength'=>50)); ?>
     <?php echo $form->error($model,'ShortDesc'); ?>
 </div>

 <div class="row">
     <?php echo $form->dropDownList($model,'CodeLookupId',Forms::model()->findAll()); ?>
 </div>

 <div class="row buttons">
     <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
 </div>

Upvotes: 0

Views: 10996

Answers (3)

KanimozhiPalanisamy
KanimozhiPalanisamy

Reputation: 63

try hope this will help..
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php
$records = loaddataform::model()->findAll();
$list = CHtml::listData($records, 'id', 'username');
echo CHtml::dropDownList('loaddataform', $model, 
      CHtml::listData($records,
      'a_id', 'a_name'),
      array('empty' => '(Select a name)'));
 ?>
 </div>


and in controller

public function actionloaddata()
{

$model = new loaddataform;
$models = loaddataform::model()->findAll(array('order' => 'a_name'));
$list = CHtml::listData($models, 'a_id', 'a_name');
$this->render('loaddata',array('model'=>$model));

}
This works correctly..it retrives name from database.

Upvotes: 0

Hitesh Modha
Hitesh Modha

Reputation: 2790

$opts = CHtml::listData(Codelookup::model()->findAll(),'CodeLookupId','CodeDesc');
echo $form->dropDownList($model,'ParentCodeLookupId',$opts,array('empty'=>''));     

This code is working

Upvotes: 2

Kumar V
Kumar V

Reputation: 8830

use CHtml::listData to convert the db object to list data:

Here ID , NAME is your table values which should be used in option value and option text.

 <?php 
    $opts = CHtml::listData(Forms::model()->findAll(),'CodeLookupId','CodeLookupId');
    //to check list
    print_r($opts);
    echo $form->dropDownList($model,'CodeLookupId',$opts); 
 ?>

Upvotes: 1

Related Questions