I have a selectbox with some values, i inserted those values inside an array.
Now i want to select some specific option, and keep the option selected even when the page reloads.
$logos =array('logo1', 'logo2', 'logo3');
echo '
<td class="jofftd">
<label>Platform</label>
<select name="searchpt">
<option value="0">All</option>
';
foreach ($logos as $value)
{
echo '
<option value="'.$value.'">' .$value . '</option>
';
}
echo '
</select>
</td>';
I would need to do something like this:
foreach ($logos as $value)
{
echo '
<option';
if ($value == $value) echo 'selected="selected"';
echo 'value="'.$value.'">' .$value . '</option>
';
}
But it doesn't work.
Thanks.
Assuming you are using a POST method form, the code would look something like this (note: not tested)
foreach ($logos as $value)
{
$selected = ($value == $_POST['searchpt']) ? ' selected' : '';
echo '<option'. $selected . ' value="'.$value.'">' .$value . '</option>';
}
If nothing else, you're missing at least one space character:
<option';
if ($value == $value) echo 'selected="selected"';
This will produce
<optionselected="selected"
which is not exactly valid HTML.
Plus, $value==$value will always be true, so even if you had proper spacing in the tags, you'd be marking ALL of the options as selected. You need to compare the $value from the loop against the value from the original form, e.g.
if ($value == $_POST['field_from_previous_form'}) { ... }
$value == $value will be always true and it will always add selected = "selected"
use string concatenation to form select form field.
<?php
$logos =array('logo1', 'logo2', 'logo3');
$value = 'logo1';
$str = '<select name="searchpt"><option value="0">All</option>';
foreach ($logos as $value)
{
$str.='<option ';
if ($value == 'logo1')
$str.=' selected="selected "';
$str.=' value="'.$value.' ">' .$value . ' </option> ';
}
$str.='</select>';
echo $str;
?>
Hope this code snippet will solve your problem
Related
I have
3 <select></select>
with 8 options inside (Array_1 with 8 position). All options value are the same.
What i am trying to do is that I want to make the Value in Array_2 to be the Selected value.
However, looks like my loop is not working correctly.
$array_1[]="value1";
$array_1[]="value2";
$array_1[]="value3";
$array_1[]="value4";
$array_1[]="value5";
$array_1[]="value6";
$array_1[]="value7";
$array_1[]="value8";
$array_2[]="value1";
$array_2[]="value3";
$array_2[]="value4";
for($i=0;$i<count($array_2);$i++){
echo '<select name="product_header_image[]">';
for($b=0;$b<count($array_1);$b++){
if(in_array($array_2[$i],$array_1)){
echo '<option selected VALUE="'.$array_1[$b].'" >'.$array_1[$b].'</option>';
}else{
echo '<option VALUE="'.$array_1[$b].'" >'.$array_1[$b].'</option>';
}
}
echo '</select>';
echo '<br>';
}
Anyone know whats wrong with my coding? what I want to achieve is the right hand side of the screenshot.
You need to replace this check:
if(in_array($array_2[$i], $array_1)) {
...
}
on this:
if($array_2[$i] == $array_1[$b]) {
...
}
Demo here
Not sure I understand your scenario, but this might be what you are looking for. It generates a dropdown with all the elements from both arrays, and highlights the selected ones.
$array_1[]="value1";
$array_1[]="value2";
$array_1[]="value3";
$array_1[]="value4";
$array_1[]="value5";
$array_1[]="value6";
$array_1[]="value7";
$array_1[]="value8";
$array_2[]="value1";
$array_2[]="value3";
$array_2[]="value4";
echo '<select name="product_header_image[]" multiple>';
foreach ($array_1 as $elem1) {
echo '<option VALUE="'. $elem1 .'" >' . $elem1 .'</option>';
}
foreach ($array_2 as $elem2) {
$selected = '';
if( in_array($elem2, $array_1) ) $selected = 'selected ';
echo '<option '. $selected . 'VALUE="' . $elem2 . '" >'.$elem2.'</option>';
}
echo '</select>';
echo '<br>';
I am trying to set selected on an <option> based on an array, and I'm close, but not quite getting there...
$departments = array("Finance", "IT", "Retail",);
foreach($departments as $list){
echo '<option';
if($found['dept'] == '$list'){ // if I set this manually it works, but not now
echo ' selected';
}
echo ' >' . $list . ' </option>'; // this works fine to show me the list
}
If I set $found[dept] manually like below, echoing 'selected' works great, but I don't want to write a version of this line for every option.
if($found['dept'] == 'Finance'){ echo 'selected';} > ' .$list . '</option>
Your variable is in single quotes which makes it a string. It's cleaner and easier to see errors like that if you break out your logic from your output.
$departments = array("Finance", "IT", "Retail",);
foreach($departments as $list){
$selected = ($found['dept'] == $list) ? ' selected' : '';
echo "<option$selected>$list</option>";
}
How can i create a Multiple select box from 2 arrays
1st array contains all values and the second array contains the values that will be marked as selected in multiple select
$a=array[1,2,3,4,5,6,7,8];
$b=array[3,7,8];
the multiple selectbox will have all the values from array $a but values from array $b will be selected.
Any way to achieve this ?
You can use this code
$a=array(1,2,3,4,5,6,7,8);
$b=array(3,7,8);
$selected="";
foreach($a as $val)
{
if(in_array($val,$b))
{
$selected = 'selected="selected"';
}
//Code for create multi select drop down and echo $selected in option like
<option $selected value="" ></option>
}
$a=array(1,2,3,4,5,6,7,8);
$b=array(3,7,8);
$html = '<select multiple>';
foreach($a as $val)
{
$selected = (in_array($val,$b)) ? 'selected' : '';
$html .= '<option value="' . $a . '"' . $selected . '>' . $a . '</option>';
}
$html .= '</select>';
echo $html;
I'm working with the Filemaker API - which is similar to sql - basically its pulling data from a fields and from a list.
I'm trying to set up an if statement inside my foreach loop so that it adds a the tag selected="selected" to my select option in my html
Here's the code
<?php
$layout =& $fm->getLayout('Leads');
$values = $layout->getValueList('LeadStatus');
$list_menu = '<select name="LeadDocStatusSelect">';
foreach ($values as $value)
{
$list_menu .='<option value="' . $value . '">' . $value . '</option>' ;
}
$list_menu .= '</select>';
echo $list_menu;
?>
How can I add an if statement like this to the foreach loop?
if ($businessJudgements == 'No') {
echo 'checked="checked"';
} else {
echo '';
}
The body of the foreach loop is simply a block of statements so you can put in as many as you want, such as with:
foreach ($values as $value) {
$list_menu .='<option value="' . $value . '">' . $value . '</option>';
if ($businessJudgements == 'No') {
echo 'checked="checked"';
} else {
echo '';
}
}
However, are you sure you have the right attribute for the option tag? If it's your intent to select a specific option, checked is not the right method (it's meant for checkbox input fields). The selected attribute is the correct one for the HTML option tag.
So you would be better off with something like:
# The default is orange.
$default_value = "orange";
# Process every option.
foreach ($values as $value) {
# Add [<option value="X"].
$list_menu .= '<option value="' . $value . '"';
# Add [ selected] ONLY for default one.
if ($value == $default_value) {
$list_menu .= ' selected';
}
# Add [>X</option>].
$list_menu .= '>' . $value . '</option>';
}
This will give you the option tags as desired but the one where the value matches the default value will also have the selected attribute attached.
Assuming $values held the array {"red", "black", "orange", "yellow"}, you would end up with (formatted nicely here for the purposes of the answer):
<option value="red"> red </option>
<option value="black"> black </option>
<option value="orange" selected> orange </option>
<option value="yellow"> yellow </option>
I assuming that you already know how to get your $businessJudgement , so that code should be something like this
<?php
$layout =& $fm->getLayout('Leads');
$values = $layout->getValueList('LeadStatus');
$list_menu = '<select name="LeadDocStatusSelect">';
foreach ($values as $value)
{
if ($businessJudgements == 'No') {
$list_menu .='<option value="' . $value . '">' . $value . '</option>' ;
} else {
$list_menu .='<option value="' . $value . '" selected >' . $value . '</option>' ;
}
}
$list_menu .= '</select>';
echo $list_menu;
?>
Normally I would do something like this:
<select name="myselect">
<option value="opt1" <?=($_POST['myselect']=="opt1"?"SELECTED":"")?>>Option 1</option>
<option value="opt2" <?=($_POST['myselect']=="opt2"?"SELECTED":"")?>>Option 2</option>
</select>
However this time I've taken a list of all the countries in a select from this website: http://snippets.dzone.com/posts/show/376
To go through each of those and put in the validation will be insane.
There is a php solution in the comments but it seems somewhat inelegant.
Is there an alternative way to do this or something similar? I would prefer not to use javascript, and I'm not sure I want to rely on the browser caching.
Thanks
// I'm using integers as key values in this example. Modify as needed.
$countries = array(
1 => 'Some Country',
// etc...
);
// Sanitize as needed, casting to integer in my example
$selectedCountryCode = isset( $_POST['myselect'] ) ? (int) $_POST['myselect'] : null;
$select = '<select name="myselect">';
foreach( $countries as $countryCode => $countryName )
{
$selected = $selectedCountryCode == $countryCode ? ' selected="selected"' : '';
$select .= '<option value="' . $countryCode .'"' . $selected . '>' . $countryName . '</option>';
}
$select .= '</select>';
echo $select;
You could create select with an array like this:
$options = array('opt1' => 'Option1', 'op2' => 'Option2');
foreach ($options as $o => $v)
{
if ($_POST['myselect'] == $o)
echo '<option value="' . $o . '" selected="selected">' . $v . '</option>';
else
echo '<option value="' . $o . '">' . $v . '</option>';
}
Add countries to value-label array
Generate options with loop
foreach ($countries as $value => $label) {
$isSelected = $_POST['myselect'] == $value ? ' selected="selected"': '';
echo '<option value="' . $value . '"' . $isSelected . '>' . $label . '</option>';
}