Variable as Array Key - Yii Dropdown Selected - php

I am using Yii and I have a dropdown using the following example:
$form->dropDownList($model,'sex',array('1'=>'men','2'=>'women'), array('options' => array('2'=>array('selected'=>true))));
Here I am able to choose which option is selected. If I set two as shown in the example above the selected option is women as expected.
I am not able to statically set the selected option as I need to use a variable. I have $selectedId which equal 2, but when doing for example:
array('options' => array("$selectedId"=>array('selected'=>true))));
or doing like this:
array('options' => array($selectedId=>array('selected'=>true))));
I am getting no errors, but the dropdown does not have the expected selected option. Is it possible to use a variable when defining an array key?
Update
True string:
CHtml::dropDownList('package','',CHtml::listData(Services::model()->findAll(array('condition'=>'is_internet = 1','params'=>array())), 'id', 'name'),array('id'=>'package'))

You can set it by setting the second parameter:
CHtml::dropDownList('package',$selectedId,CHtml::listData(Services::model()->findAll(array('condition'=>'is_internet = 1','params'=>array())), 'id', 'name'),array('id'=>'package'))

Related

CakePHP 2: Using stored array as keys in a $this->Model->find statement

I have a dropdown on my site that allows for multiple selections:
$this->Form->input('systems', array('label'=>'System Assignments', 'empty'=>'', 'default'=>'', 'div'=>false, 'multiple'=>true, 'class'=>'chosen',
'options'=>$systems));
This code from my controller populates the $systems variable:
$systems = $this->Discrepancy->find('list', array('fields' => array('id', 'description'),
'conditions'=>array('Discrepancy.deleted_record' => 0),
'order'=>array('Discrepancy.display_order'=>'ASC')));
$this->set(compact('systems'));
When the user makes their selection(s), the row ID's are stored as an array in a table called Users in a field called systems.
$system_string = implode(',', $this->request->data['User']['systems']);
$this->request->data['User']['systems'] = $system_string;
systems
-------
50,22
On my Edit screen, I would like to be able to use that array of values as my list of ID's for retrieving and displaying the user's system choices, by adding a 'value' parameter to the dropdown.
'value'=>array_key($chosen_systems);
How can I use these stored values in a $this->Model->find statement?
Got it. Guess I needed to write it all out first.
Put the following line in the $this->Model->find statement:
'conditions'=>array('Discrepancy.id'=>explode(',',$var_for_systems_from_db))

Selected option with form::select

I want to make a dropdown list of an array with the method Form::select. The dropdown is dynamically generated so the selected item can change.
I made this code :
echo Form::select('nomselect',
$noms_sols,
$_GET['id_region'],
array('onchange'=>"",
'id' => 'select_sols',
'selected' => $systeme['nom_sol']));
The dropdown is working but I don't have the selected item that I want. On the last line of the code, I tried something, but it doesn't work.
Is there a way to do it ? Or am I forced to use a foreach method ?
Thanks in advance.
Third parameter of Form::select method is a selected item. In your case value from $_GET['id_region'] should be also in $noms_sols array.
For example this should add selected attribute on option with value 2.
$noms_sols = array("1", "2", "3");
$id_region = $_GET['id_region']; // $id_region == "2"
echo Form::select('nomselect',
$noms_sols,
$id_region);

Form::model binding Laravel with <select multiple>

I just discorvered that Form::model binding existed and I'm delighted with (it's wonderful). I tried it with text, email, and even select, every single time it worked.
My question is, will it work with a <select multiple>? If it does, how am I supposed to use it and what is the correct way to save an array in the database? (This may be awful but I concatenate all the options of the array with a separator and save it as text, I'm sure it's not the correct way to do it).
just like this :
Form::select('menus[]', $menus, null, array(
'multiple' => true,
'class' => 'form-control'
));
take a note :
param 1 : should be your field name (if want multiple add array tag aftrer field name e.g :menus[])
param 2 : list menu (array) e.g : array('value1' => 'text1', 'value2' => 'text2')
param 3 : selected values. (should be null because Form::model will perform automatically matching values from database. and make sure the field name has same with key data result from database)
param 4 : is property for element <select> you can add class, id, and etc..

how to use selected option of form select element in zend framework2?

I am using Zend\Form\View\Helper\FormSelect and I did not find the way to set selected value in select element.
$countryList = array_merge (array('empty_option' => 'Please choose...'),
$this->common()->getCountryList() );
$country->setValueOptions($countryList);
$country->setValue(array('AT' => 'AUSTRIA'));
echo $this->formSelect($country);
$country->setValue('AT');
As above, you can use setValue() for making 'AT' as selected.

cakephp showing values instead of int

I have an int field in my cakephp project which translates into some values like here:
On my add
echo $form->input('bid_type', array('options' => array(
'1'=>'CPC',
'2'=>'CPM', )));
But when viewing this field i dont want the integers shown but the values.
How can i translate these values into strings when loaded?
if you don't want integers just change the keys
echo $form->input('bid_type', array('options' => array(
'CPC'=>'CPC',
'CPM'=>'CPM', )));
this will give you CPC when selecting CPC etc... also the "values" are the keys of the array so technically it was giving you the values. The value of the array is just the display of the option, you may change it to whatever you need

Categories