Selected option with form::select - php

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);

Related

Selected values for multiple select on Laravel Collective

I've got a string value from a controller which is:
$pickedValues = "1,2";
So what I'm trying to do is set the multiple selected values on the view, here is the select code that I have right now:
{{Form::select("selection[]", $data, (isset($pickedValues)) ? array($pickedValues) : '',
[
"class" => "multiple-select",
"multiple",
])
}}
That problem that I'm getting right now that it is not displaying the selected options, but if I put the values manually like array(1,2) it works. I've tried setting it as array((int)$pickedValues) but it only displays the value 1.
I found a solution to this problem,
Try the value like this
array('1','2')

Variable as Array Key - Yii Dropdown Selected

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'))

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.

Select multiple default, or previously chosen, values in a select box in osCMax

I am using the following method for displaying the multiple select box and it works fine in the new form.
echo tep_draw_pull_down_menu("cat[$i][]", $cat_array, '', 'id=cat3, size=5 multiple');
But in the edit form I want the values to be selected by default which are inserted in the add form.
The values which are to be selected by default are stored in an array. So I passed the array to the default value like the following.
echo tep_draw_pull_down_menu("cat[$i][]", $cat_array, "$arr", 'id=cat3, size=5 multiple');
But it did not selected the required values. It only selected one value at a time because we cannot pass an array to the default value.
How will I do this?
You're not going be able to without changing some of the codebase.
By default, the tep_draw_pull_down_menu method is only going to allow and check for strings. It's not at all expecting to be passed more than one value to check against.
Edit the tep_draw_pull_down_menu method found in the includes/functions/html_output.php by looking at this line (around 312 of a clean install):
if ($default == $values[$i]['id']) {
Change it to the following:
if ($default == $values[$i]['id'] || (is_array($default) && in_array($values[$i]['id'], $default))) {
The extra bit adds a check to see if the $default variable passed was an array, and if so, if the current value of the select option is in there. If yes on both counts, then mark the option as selected.
Here's an example of it in use:
$cat_array[] = array("id" => 'marvelman', "text" => 'Kimota!');
$cat_array[] = array("id" => 'rorschach', "text" => 'hurm');
$cat_array[] = array("id" => 'cerebus', "text" => 'Something Fell');
$cat_array[] = array("id" => 'wolvie', "text" => 'Snikt!');
$cat_array[] = array("id" => 'spider-man', "text" => 'Thwip');
$arr = array('rorschach', 'wolvie', 'cerebus');
echo tep_draw_pull_down_menu("cat[$i][]", $cat_array, $arr, 'id="cat3" size="5" multiple');
You may also not want to pass the $arr variable in quotes unless you explicitly want to pass the string $arr as opposed to its array of values.

Traverse ez-multioption attribute options in EZ Publish PHP-side

I need help to traverse all options in a multioption.
I use the Product-class with a new multioption-attribute called "product_properties". I need a function to check if the optionID the user chose on the front-end matches an option in the list, and return true if a match is found.
This way I can check if e.g. the user chose "Red" as the "Color" on a product.
In pseudo-code this is what I need:
Parameters: postedOptionID, currentObjectID
Fetch attribute "product_properties" (multioption) on object .
For each option for "Color" in "product_properties"
2.1 If postedOptionID == optionID
2.1.1 return true
Thanks
I finally found a way :)
$product_properties_name is the name of a class-attribute that's an 'ezmultioption'-datatype. In my case it's called 'product_properties' and is an attribute on the 'Product'-class.
First get all of the object's attribute:
$contentObjectAttributes = $contentObject->version($contentObject->attribute( 'current_version' ) )->contentObjectAttributes();
and then loop each and find 'product_properties':
// Loop all attributes of the object's class
foreach(array_keys($contentObjectAttributes) as $key)
{
$contentObjectAttribute = $contentObjectAttributes[$key];
$contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
$attributeIdentifier = $contentClassAttribute->attribute("identifier");
// Get 'product_properties'-attribute
if ($attributeIdentifier == $product_properties_name)
{
// Get the multioption
$multioption_list = $contentObjectAttribute->content();
// Loop all multioption lists (Color, Make, Brand etc.)
foreach($multioption_list->attribute('multioption_list') as $index => $option)
{
// Loop through this multioption and get all options (if 'Color', get 'Blue', 'Red', 'Green' etc.)
foreach($option['optionlist'] as $option)
{
$optionValue = trim($option['value']);
// if there's a match on $optionValue, do something interesting...
}
}
}
}

Categories