I want to add a dropdown list in my page which should show the values from a table column, How can I do this using Yii framework?
I have a table(table name is program) with columns
id, program_name, is_active
I have created a new controller and a view associated with it, I need to show a dropdown list in that view with the values populated from program_name
I would solve this at model level. e.g.
In Machine model, I would define a getter :
public function getCompleteMachineName ()
{
return $this->merk->name.' '.$this->name;
}
And, in your listData :
Chtml::listData(Machine::model()->with('merk')->findAll(...),
'machine_id', 'completeMachineName')
All you need to do is use CHtml::dropDownList, possibly with CHtml::listData to ease the process of creating a value=>display array for the <options> tag.
Example (see comments in code):
echo CHtml::dropDownList(
'somename',// for "name" attribute of <select> html tag,
// this also becomes the "id" attribute, incase you don't specify
// it explicitly in the htmlOptions array
'', // the option element that is to be selected by default
CHtml::listData( // listData helps in generating the data for <option> tags
Program::model()->findAll(), // a list of model objects. This parameter
// can also be an array of associative arrays
// (e.g. results of CDbCommand::queryAll).
'id', // the "value" attribute of the <option> tags,
// here will be populated with id column values from program table
'program_name' // the display text of the <option> tag,
// here will be populated with program_name column values from table
),
array('id'=>'someid'), // the htmlOptions array, whose values will be
// generated as html attributes of <select> and <option> tags
);
Edit Incase you don't have a CActiveRecord model for program table, you could use direct sql, replace Program::model()->findAll() in the above sample with:
Yii::app()->db->createCommand()->select('id, program_name')->from('program')->queryAll()
Also if you want to use program_name column values as the value attribute for the option tag, then you can use:
CHtml::listData($data,'program_name','program_name') // replaced the 'id' with 'program_name'
Related
I have made a webform using the sourcecode from Advanced Webform for Podio . I have a list made from a referenced categoryfield. When an item from the list is selected and the form is submitted, I want the text value from the referenced category field to be copied into the an other textfield.
After submit and before saving the data into Podio I can fetch the data from the
post array.
if ($_POST){
// all the values are stored in $_POST array
Then I get the value from the category field like this:
$value_from_selected_list = $_POST['uu center'];
After this I copy the value to the new text field
$_POST['organisation'] = $value_from_selected_list;
// set all the values in the Podio app
$podioform->set_values($_POST,$_FILES);
$podioform->save();
$podioform = 'Kære '.$_POST['navn'].' Tak for din tilmelding';
}
My problem is that $_POST['organisation'] only returns an id and not the text value.
I found the solution myself. $_POST['uu-center']
holds the item_id of the referenced app. So I found the field_id of the the referenced field and used the get_field_value method that takes the item_id and field_id as arguments
$item_id = $_POST['uu-center'];
$field_id = 154492804;
$uucenter = PodioItem::get_field_value($item_id,$field_id);
The method returns an array with the selected value of the field. Then I got the text string.
$value_to_be_copy = $uucenter[0][value];
I have following code in my view
<?php echo $form->dropDownList($model,'p_28',
CHtml::listData(AdCourt::model()->findAll(),'id','name') ,
array(
'class'=>'form-control',
'style'=>'width:100%;',
'empty'=>Yii::t('plaint','Суд турини танланг'),
'ajax' => array(
'type'=>'POST', //request type
'url'=>$this->createUrl('getcourts'),
'update'=>'#ABlockForm_p_29',
)
)); ?>
AdCourt takes values from ad_court table. This tables consists of id, name and court_id. I need to save database court_id instead of id.
How can I do it?
The second parameter of listData is the attribute of the items that will be used as the value for the <option> tag. As such to use the court_id instead of the id you should pass it instead:
CHtml::listData(AdCourt::model()->findAll(),'court_id','name')
I have a Product model with a 'categories' column. This column should be able to contain data from a Checkboxlist.
Whenever a user creates a new Product I want the form to display the 'categories' Checkbox, with the items set as active from the user's last created Product.
So for instance: the 'categories' Checkboxlist contains items 'Movies' and 'Music'. When the user checks 'Movies' and creates the Product, the next time the user goes to create a Product, 'Movies' is already selected (because it saves the selection from the user's previous product creation).
I believe these are the most effective steps to code in order to achieve this goal:
When Product is created: checked items are saved to 'categories' column in Product Model & a 'categories' column in the User's 'Profile' model
User's last saved categories ('categories' column in Profile model) should be retrieved at the Product's Create form.
Code in Product model's _form view:
<?php echo $form->checkBoxList($model, 'categories', array('movies'=>'Movies','music'=>'Music')); ?>
(I'm unsure as to where to set an array for active values)
I'll need to convert the Array of the selected Checkboxes to a string using explode(",", $model->categories) so I can put it inside the 'categories' columns of the 'Product' and 'Profile' models by using the ProductController's actionCreate function.
Then to set the user's last selected Checkboxlist items as active in the Product _form view I need to convert the $model->categories data back to an array by imploding the column ie implode(",", $user->profile->categories).
How would you code this in Yii?
Use CHtml::ckeckboxList instead of activecheckbox.
It have selected parameter. Selected there is array of key=>value pairs. yiiframework.com/doc/api/1.1/CHtml#checkBoxList-detail
Or you can rewrite (extend) you CHtml helper to resolve your format.
For example:
public static function activeCheckBoxList($model,$attribute,$data,$htmlOptions=array())
{
self::resolveNameID($model,$attribute,$htmlOptions);
if(array_key_exists('explode_format',$htmlOptions))
{
$selection=explode($model->$attribute);
unset($htmlOptions['explode_format']);
}
else{
$selection=self::resolveValue($model,$attribute);
}
if($model->hasErrors($attribute))
self::addErrorCss($htmlOptions);
$name=$htmlOptions['name'];
unset($htmlOptions['name']);
if(array_key_exists('uncheckValue',$htmlOptions))
{
$uncheck=$htmlOptions['uncheckValue'];
unset($htmlOptions['uncheckValue']);
}
else
$uncheck='';
$hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false);
$hidden=$uncheck!==null ? self::hiddenField($name,$uncheck,$hiddenOptions) : '';
return $hidden . self::checkBoxList($name,$selection,$data,$htmlOptions);
}
Then just add 'explode_format'=>true to your htmloptions for activecheckboxlist. Something like this will work.
I am building a form where users will need to be able to add multiple 'children' and fill in details for them. Each child has multiple inputs so to keep it simple we'll say a user is trying to add 2 children and each child requires a first name and a surname.
I aim in using javascript to add the children to a list so a user can enter a first name and surname then press 'add' and this will create hidden inputs for these details. What I am struggling with is geting these form inputs to be grouped by child so effectively what I end up with will be a $_POST array like the following:
$_POST['child'][0]['firstname'] = 'asdads';
$_POST['child'][0]['surname'] = 'vcbcvbc';
$_POST['child'][1]['firstname'] = 'asdads';
$_POST['child'][1]['surname'] = 'vcbcvbc';
Is it as simple as just setting the 'name' attribute on the inputs to name="child[][firstname]" or can someone offer a suitable solution?
javascript:
var children = [[{name:value},{name:value}],[{name:value},{name:value}],....];
php:
for($i=0; $i<sizeof($_POST['children']); $i++)
{
foreach($_POST['children'][$i] as name => value)
{
// process
}
}
I am generating options for my dropdown using
// view.php
foreach ($plants as $row):
$options[$row->plant_id] = $row->plant_name;
endforeach;
and then lower in the HTML part of view.php
//view.php
$js = 'onChange = "plantDateDelete(\'/size/get_dates_for_plant/\'+this.value);"';
echo form_dropdown('plant_id', $options, 'Select', $js);
The dropdown show options OK, but it does NOT show 'Select' as the "selected"/default value. It shows up with the first option of the array instead.
The HTML source also shows 'Select' in form_dropdown was ignored.
I really need this dropdown to show up with 'Select' as default so as to force the user to activate the onChange function.
Any idea what is going on here or how to solve this issue?
You need to have the default option in your $options array...
So before your foreach, do:
$options = array('Select');
I should add, this will have the value of 0 in the dropdown, but as its the first element in the array it will be selected by default, unless another option is passed as the default value.
If you wanted to explicitly set this value as the default you would pass 0 as the default argument.