Removing duplicated options from select box in PHP - php

I want to generate select boxes based on the number of elements in the $chosen array and each of them will have the default selected options. The first one is A, and the second one is D. The following code is almost okay except I want to remove the duplicated options. Here's the output:
<select>
<option>Select</option>
<option value="A" selected="selected">A</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
</select>
<select>
<option>Select</option>
<option value="D" selected="selected">D</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
</select>
I tried array_merge with array_unique to merge $options and $chosen before foreach($options as $option) but it isn't working. Can anyone suggest a solution?
<?php
$options = array("A","B","C","D","E");
$chosens = array("A","D");
foreach($chosens as $chosen)
{
print "<select><option>Select</option>";
if(in_array($chosen,$options))
{
print "<option value='".$chosen."' selected='selected'>$chosen</option>";
}
/* $options = array_unique(array_merge($chosen,$options)); */
foreach($options as $option)
{
print "<option value='".$option."'>$option</option>";
}
print "</select>";
}
?>*

Actually, just a simple if condition is enough. Like this:
<?php
$options = array("A","B","C","D","E");
$chosens = array("A","D");
?>
<?php foreach($chosens as $chosen): ?>
<select name="">
<?php foreach($options as $option): ?>
<option value="<?php echo $option; ?>" <?php echo ($option == $chosen) ? 'selected' : ''; ?>><?php echo $option; ?></option>
<?php endforeach; ?>
</select><br/>
<?php endforeach; ?>

Related

Form select option selected PHP

I try to find the way how to make option tag selected if contain current day.
For example if my string $currentDay=03; called by PHP date('d'); I would like my select like this:
<select>
<option value="01">01</option>
<option value="02">02</option>
<option value="03" selected>03</option>
<option value="04">04</option>
...
...
</select>
<?php $day=date('d');?>
<select>
<option value="01" <?=($day=='01')?'selected':'';?> >01</option>
<option value="02" <?=($day=='02')?'selected':'';?> >02</option>
<option value="03" <?=($day=='03')?'selected':'';?> >03</option>
<option value="04" <?=($day=='04')?'selected':'';?> >04</option>
...
...
</select>
You can use the following code:
$day = date('d');
$selected = ' selected="selected" ';
<select>
<option value="01" <?php if($day=='01') echo $selected; ?>>01</option>
<option value="02" <?php if($day=='02') echo $selected; ?>>02</option>
<option value="03" <?php if($day=='03') echo $selected; ?>>03</option>
<option value="04" <?php if($day=='04') echo $selected; ?>>04</option>
...
...
</select>
<select>
<? foreach ($options as $option) : ?>
<option value="<?= $option ?>" <? if ($selected_option==$option) echo 'selected' ?>>
<?= $option ?>
</option>
<? endforeach ?>
</select>

Set a PHP variable as the value of user selected dropdown form

I'm trying to create a form with text fields and dropdowns. With the text fields, I'm using this code:
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo $user->name;?>" style="width:95%;"/>
The form then checks if the user has filled out the "name" field with this:
$name = $input->get('name', null);
if($name == null)
return false;
This works fine for the multiple text fields that the form uses, but I don't know how to check for the user input on the dropdowns. How can I send the option selected by the user, similar to the way I did it with the text field, so that I can check to make sure the user selected something? Example:
<label for="department">Department</label>
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4"><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
$department1 = $input->get('department1', null);
$department2 = $input->get('department2', null);
Etc........
This is set up the exact same way as the text fields as far as I know, but doesn't work and seems like a bad way to do it anyways.
if you want to verify and pre-select one option you should do something like this:
<option value="6" <?php if($params->get('department6')==true){ echo 'selected'; } ?>><?php echo $params->get('department6');?></option>
First of all, instead of having 8 lines like that for your departments, you could use a for loop.
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>"><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
Now, inside your for loop, you can add a check if the $input->get is true.
<?php if($input->get('department' . $i, null)) echo 'selected'; ?>
So if you mix both together, the result would be
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>" <?php if($input->get('department' . $i, null)) echo 'selected'; ?>><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
And if you want to a "pre selected" option use "selected" in the option you want:
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4" selected><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
As explained by W3CSchools.

Error setting the selected value from a select list

I have a HTML select list where one can chose an option.
<select name='price' id='price'>
<option value='' >Select....</option>
<option value='0-50,000' selected>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
When this list is submitted via a HTML submit button, this list shows again in another page. Now, I want the option the user selected to be new selected value. I am doing this:
<select name='price' id='price'>
<option value='{$_POST['price']}'>{$_POST['price']}</option>
<option value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
But values are appearing twice. The option the user selected is shown as the selected and still appears in the list. For example, we now have something like this:
0-50,000 (this is the selected value)
0-50,000
50,000-100,000
100,000-150,000
150,000-200,000
200,000 and above
How do I solve this?
In other page, make sure the selected option has selected="selected"
<select name='price' id='price'>\
<option value='' >Select....</option>
<option selected="selected" value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
try this
/**
* Takes To values (First for option value Second for value to be compare)
* #param Srting $option stores Option Value String
* #param String $value Stores to be compare
* #return String
* #access public
*/
function selectBoxSelection($option, $value) {
if ($option == $value) {
return "selected";
}
}
<select name='price' id='price'>
<option value='0-50,000' <?php echo selectBoxSelection('0-50,000', $_POST['price']);?> >0-50,000</option>
<option value='50,000-100,000' <?php echo selectBoxSelection('50,000-100,000', $_POST['price']);?> >50,000-100,000</option>
<option value='100,000-150,000' <?php echo selectBoxSelection('100,000-150,000', $_POST['price']);?> >100,000-150,000</option>
<option value='150,000-200,000' <?php echo selectBoxSelection('150,000-200,000', $_POST['price']);?> >150,000-200,000</option>
<option value='200,000 and above' <?php echo selectBoxSelection('200,000 and above', $_POST['price']);?> >200,000 and above</option>
<option value='see all' <?php echo selectBoxSelection('see all', $_POST['price']);?> >See All</option>
</select>
instead of your code,
<select name='price' id='price'>
<option value='{$_POST['price']}'>{$_POST['price']}</option>
<option value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
try like this,
<select name='price' id='price'>
<option value='' >Select....</option>
<?php foreach ($arr as $key=>$value)
{
if($key == $_POST['price'])
{
echo "<option selected value=$key>$value</option>";
unset($arr[$key]);
}
else
echo "<option value=$key>$value</option>";
}?>
</select>
This will do it :
<form name="myform" method="post" action="testsel.php">
<select name='price' id='price'>
<option value='' >Select....</option>
<option value='0-50,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="0-50,000") print "selected"; ?>>0-50,000</option>
<option value='50,000-100,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="50,000-100,000") print "selected"; ?>>50,000-100,000</option>
<option value='100,000-150,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="100,000-150,000") print "selected"; ?>>100,000-150,000</option>
<option value='150,000-200,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="150,000-200,000") print "selected"; ?>>150,000-200,000</option>
<option value='200,000 and above' <?php if(isset($_POST["price"]) && $_POST["price"]=="200,000 and above") print "selected"; ?>>200,000 and above</option>
<option value='see all' <?php if(isset($_POST["price"]) && $_POST["price"]=="see all") print "selected"; ?>>See All</option>
</select>
<input type="submit" value="submit">
</form>
<?php
if(isset($_POST["price"]))
print $_POST["price"];
?>
Save this file as "testsel.php" and view. It will serve the purpose.
<select name='price' id='price'>
<option value='' >Selec</option>
<option value='0-50,000'<?php if($_POST['price']=='0-50,000'){echo "selected==selected";}?>>0-50,000</option>
<option value='50,000-100,000'<?php if($_POST['price']=='50,000-100,000'){echo "selected==selected";}?>>50,000-100,000</option>
<option value='100,000-150,000'<?php if($_POST['price']=='100,000-150,000'){echo "selected==selected";}?>>100,000-150,000</option>
<option value='150,000-200,000'<?php if($_POST['price']=='150,000-200,000'){echo "selected==selected";}?>>150,000-200,000</option>
<option value='200,000 and above'<?php if($_POST['price']=='200,000 and above'){echo "selected==selected";}?>>200,000 and above</option>
<option value='see all'<?php if($_POST['price']=='see all'){echo "selected==selected";}?>>See All</option>
</select>

PHP Mysqli HTML How to have current value from database selected in <select>

my goal is get current value from database, selected in the dropdown list.
I tried that code but it shows always "Ndone" in the dropdownlist
<select name="work" id="work" value="<?php echo $work; ?>">
<option selected="selected" value=""></option>
<option selected="selected" value="DONE">DONE</option>
<option selected="selected" value="NDONE">NDONE</option>
</select>
Also this one and nothing just the first row that selected in the dropdownlist
<select name="work" id="work" value="<?php echo $work; ?>">
<option value=""></option>
<option value="DONE">DONE</option>
<option value="NDONE">NDONE</option>
</select>
I don't know what to do, any help please
When i use a textfield it works, so there's no problem with the variable $work
<input type="text" name="work" value="<?php echo $work; ?>"/>
<select> does not have a "value" Attribute. You must add the selected attribute to one option.
<?php
$options = array(
'',
'DONE',
'NDONE',
);
?>
<select name="work" id="work">
<?php foreach ($options as $option): ?>
<option value="<?php echo $option ?>"<?php echo $option === $work ? ' selected="selected"' : '' ?>><?php echo $option ?></option>
<?php endforeach ?>
</select>
As per your example you could do this:
You just need to check each option value against the $work variable and if it matches add the selected attribute.
<select name="work" id="work">
<option value=""></option>
<option value="DONE" <?php echo($work == 'DONE'?'selected="selected"':''); ?>>DONE</option>
<option value="NDONE" <?php echo($work == 'NDONE'?'selected="selected"':''); ?>>NDONE</option>
</select>

how to retrieve data in multiple selected list

i need to retrive data in mutiple selected list from database
in my database value is
Marathi,Arunachali,Assamese,Awadhi,Marathi,Arunachali,Assamese,Awadhi
i used the below code to retrive
<select id="spokenlanguages" name="known_languages[]" multiple="multiple" size="5">
<option value="" >Select</option>
<?php
$langs = $editdatapersonaltbl[0]['known_languages'] ;
$known_languages = explode(",",$langs);
//print_r($known_languages); exit();
for($i=0;$i < count($known_languages);$i++)
{
?>
<option value="<?php echo $known_languages[$i] ;?>" selected="selected"><?php echo $known_languages[$i] ;?></option>
<?php } ?>
<option value="Marathi" >Marathi</option>
<option value="Arunachali" >Arunachali</option>
<option value="Assamese" >Assamese</option>
<option value="Awadhi" >Awadhi</option>
<option value="Bengali" >Bengali</option>
<option value="Bhojpuri" >Bhojpuri</option>
<option value="Brij" >Brij</option>
<option value="Bihari" >Bihari</option>
</select>
but my problem is if marathi is previously selected than it displayed in selected and
not selected also
not selected because of <option value="Marathi" >Marathi</option> option is there if i will remove this option <option value="Marathi" >Marathi</option> than if marathi is not in database than what if user want to select marathi
please provide me some solutions....
Try this:
<select id="spokenlanguages" name="known_languages[]" multiple="multiple" size="5">
<option value="" >Select</option>
<?php
$langs = $editdatapersonaltbl[0]['known_languages'] ;
$known_languages = explode(",",$langs);
$known_languages = array_filter( $known_languages ); #remove the blank values if any
//print_r($known_languages); exit();
?>
<option value="Marathi" <?php if(in_array('Marathi', $known_languages)){echo 'selected="selected"';}?> >Marathi</option>
<option value="Arunachali" <?php if(in_array('Arunachali', $known_languages)){echo 'selected="selected"';}?>>Arunachali</option>
<option value="Assamese" <?php if(in_array('Assamese', $known_languages)){echo 'selected="selected"';}?>>Assamese</option>
<option value="Awadhi" <?php if(in_array('Awadhi', $known_languages)){echo 'selected="selected"';}?>>Awadhi</option>
<option value="Bengali" <?php if(in_array('Bengali', $known_languages)){echo 'selected="selected"';}?>>Bengali</option>
<option value="Bhojpuri" <?php if(in_array('Bhojpuri', $known_languages)){echo 'selected="selected"';}?>>Bhojpuri</option>
<option value="Brij" <?php if(in_array('Brij', $known_languages)){echo 'selected="selected"';}?>>Brij</option>
<option value="Bihari" <?php if(in_array('Bihari', $known_languages)){echo 'selected="selected"';}?>>Bihari</option>
</select>
first u must put html options in array as html_aarray , then cho database array . after that in another while eho html option if the node of html_aarray not exists in database array.
i wrote it 4 u , use this :http://codepad.org/gjWW9UGg
<?php
//$lang_array is ur language array
$lang_array = array('Marathi','Hindi','Gujrati','Bengali');
//this array ehich u fetching from DB
$known_languages = array('Marathi', 'Gujrati');
?>
<select id="spokenlanguages" name="known_languages[]" multiple="multiple" size="5">
<option value="" >Select</option>
<?php
for($i=0;$i < count($lang_array); $i++)
{
if(in_array($lang_array[$i], $known_languages))
{
$str = 'selected="selected"';
}
else
{
$str ='';
}
echo
'<option value="'.$lang_array[$i].'" '.$str.'> '.$lang_array[$i].'</option>';
}
?>

Categories