PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 9, 2022

[FIXED] Select or Dropdown list from CActiveRecord in Yii

 January 09, 2022     php, yii, yii1.x     No comments   

Issue

I have table types and i want to build selectbox with all values from this table In my controller i wrote this code

$allRegistrationTypes = RegistrationType::model()->findAll();
$this->render('index', array('allRegistrationTypes' => $allRegistrationTypes))

How build selectbox in view file ?


Solution

Well then its pretty simple all you need to do is first create List Data like

CHtml::ListData(allRegistrationTypes,'value you want to pass when item is selected','value you have to display');

for ex

typeList = CHtml::ListData(allRegistrationTypes,'id','type');

now remember both id and type are fields in table

now all you have to do is if you are using form then

<?php echo $form->dropDownList($model, 'type_id', $typeList, array('empty'=>'Select a tyoe')); ?>

and if you need multiple you can pass multiple => multiple in the array as htmlOptions



Answered By - Afnan Bashir
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing