PHP with dropdown selection - php

I have list of items in my dropdown. Now I wish to select item according to database value. Suppose I have list of cities their are nearly 300 cities. I have city value as new jersey in my record. Now while editing form checking each options is not possible for 300 options. Like
<option value="A" <? if ($a="A") echo "selected"; ?> >A</option>
<option value="B" <? if ($a="B") echo "selected"; ?> >B</option>
<option value="C" <? if ($a="C") echo "selected"; ?> >C</option>
<option value="D" <? if ($a="D") echo "selected"; ?> >D</option>
<option value="E" <? if ($a="E") echo "selected"; ?> >E</option>
<option value="F" <? if ($a="F") echo "selected"; ?> >F</option>
<option value="G" <? if ($a="G") echo "selected"; ?> >G</option>
<option value="H" <? if ($a="H") echo "selected"; ?> >H</option>
Isn't any way to minimise for 300 records. Can't I write short code for this?
** NOTE My options values are not stored in array.**

suppose your values are stored in an array called $options, you could write something like this:
foreach($options as $option)
{
echo "<option value=\"$option\"";
if($option == $a)
{
echo ' selected="selected"';
}
echo ">$option</option>";
}

you should use loops for output
like
$result = mysql_query('select id, city from cities');
while($row = mysql_fetch_assoc($result)) {
echo '<option value=' . $row['id'] . (($row['city'] == 'new jersey') ? 'selected' : '') . " >" . $row['city'] . '</option>';
}

Try range function in php:
<?php foreach (range('A', 'Z') as $letter){?>
<option value="<?php echo $letter?>" <? if ($a==$letter) echo "selected='selected'"; ?> ><?php echo $letter?></option>
<?php }?>
Change the range accordingly. You could use this function with integers too.

Related

How i can list selected value as selected and other values as listin php foreach loop

I want to display selected value as selected and others list in drop down.how it is possible inforeach loop
<?php $physicaldata = explode(",",$physical);
?>
<select name="PEPhysical[]" multiple onchange="GetrestPhysical(this.value);">
<?php foreach ($physicaldata as $physical)
{?>
<option <?php if($physical == 'Normal'){ echo ' selected="selected"'; } ?> value="Normal" >Normal Person</option>
<option<?php if($physical == 'Deaf/Dumb'){ echo ' selected="selected"'; } ?> value="Deaf/Dumb">Deaf/Dumb</option>
<option<?php if($physical == 'Blind'){ echo ' selected="selected"'; } ?> value="Blind">Blind</option>
<option<?php if($physical == 'Physically Challenged'){ echo ' selected="selected"'; } ?> value="Physically Challenged">Physically Challenged</option>
<option <?php if($physical == 'Mentally Challenged'){ echo ' selected="selected"'; } ?>value="Mentally Challenged">Mentally Challenged</option>
<option <?php if($physical == 'With other Disability'){ echo ' selected="selected"'; } ?> value="With other Disability">With other Disability</option>
<?php } ?>
but now listing morethan onetime(look uploaded image)
how i can change listing value display ones.?
Either do the foreach over possible values, and check whether the value is in selected values (only one option tag in your code then since you'r printing it in a loop):
<?php foreach ($possibleValues as $possibleValue)
{?>
<option <?php
if($selectedValues[$possibleValue->id]){ echo ' selected="selected"'; }
?> value="<?php
echo $possibleValue->id
?>" ><?php
echo $possibleValue->description
?></option>
<?php } ?>
Or print them as you do, but then don't use a for loop to print them - use a loop or better an associative array to check whether each of them is selected.

echo selected in chosen select

I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>

HTML select option set selected default echo get value php

I have a simple HTML form with a simple select like this
<select name="myselect" selected="<?php echo $_GET['brand'];?>">
<option value="" <?php if($brand== "") echo "selected"; ?>>all brands</option>
<option value="samsung" <?php if($brand== "samsung") echo "selected"; ?>>Samsung</option>
<option value="motorola" <?php if($brand== "motorola") echo "selected"; ?>>Motorola</option>
</select>
What I am trying to do is because its a search filtering results form if the user choose Samsung as default brand to search next page with filteres search results will the select option default selected Samsung and no other. If user select to search Motorola then the selected option default will be Motorola.
How do I fix this?
selected is attribute of option not select, so remove it from select tag, change to:
<?php $brand = trim( strtolower($_GET['brand']) ); ?>
<select name="myselect">
<option value="" <?php if($brand== "") echo "selected"; ?>>all brands</option>
<option value="samsung" <?php if($brand== "samsung") echo "selected"; ?>>Samsung</option>
<option value="motorola" <?php if($brand== "motorola") echo "selected"; ?>>Motorola</option>
</select>
This is Simple Example of selected=selected by using foreach loop and ternary operators in php... use can easily understand and customize that code...
<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $key => $value) { ?>
<option value="<?php echo $key;?>" <?php echo ($key == '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>
<?php $selected_brand = $_GET['brand'];
$temp_str = '';
$temp_str .= '<select name="myselect">';
$brands_array = array("all" => "all brands", "samsung" => "Samsung", "motorola" => "Motorola");
foreach ($brands_array as $key => $value) {
if($key == $selected_brand){
// For selected option.
$temp_str .= '<option value="'.$key.'" selected>'.$value.'</option>';
} else {
$temp_str .= '<option value="'.$key.'">'.$value.'</option>';
}
}
$temp_str .= '</select>';
echo $temp_str; // Select box.
?>

How to select the selected option field in dropdown in php

Consider the following command:
echo '<option value="', $row['name'], '">', $row['name'], '</option>';
Now how do I add the following code into the code I'm doing does not work either.
if ($row['name'] =='class2' )echo " selected ";
Try
if ($row['name'] =='class2' ){echo "selected=\"selected\"";}
edit: I hope I got your point right with that less informations...
just to put it right for you:
<select>
<option
<?
if ($row['name'] =='class2' )
{echo "selected=\"selected\"";}
?> value="<?=$row[name]?>">
<?=$row[name]?>
</option>
</select>
Do you mean something like this ?
<option <?php echo $row['name']=='class2'?"selected='selected'":""; ?>
value="<?php echo $row['name'] ?>">
<?php echo $row['name'] ?>
</option>

Making an option in an HTML dropdown menu selected.

Thanks for reading my question.
I have a website that uses PHP for a searchable card database. Right now I have the code for the head/searchbox section on each page, but I want to use functions instead. The function itself is working. The problem is with the drop down boxes. I currently have it set up so that when a user selects and option, and then searches, the selections are selected on the next page.
I am sure that I am having a problem with syntax.
Here is an example of the working code, which is used on the live site right now.
<select name="Type" onchange="this.submit()">
<option value="1" >[All Card Types] </option>
<option value="Hero" <?php if($_GET['Type'] == "Hero") echo "selected='selected'"; ?>> Hero </option>
<option value="Ally" <?php if($_GET['Type'] == "Ally") echo "selected='selected'"; ?>> Ally </option>
</select>
This is the relevant code from the function, which is not working, and is on the test site (ignore the error):
function searchBox(){
//Cell 1
echo '
<select name="Type" onchange="this.submit()">
<option value="1" >[All Card Types] </option>
<option value="Hero" <?php if($_GET["Type"] == "Hero") { echo "selected=selected" ; }?>> Hero </option>
<option value="Event" <?php if($_GET["Type"] == "Event") { echo "selected=selected"; }?>> Event </option>
</select>
';
}
As you can see in the test page, the dropdown menu doesn't function like it does on the live page.
Thanks for the help!
You can't embed <?php tags in a string like that. You have to concatenate it with ternary operators.
function searchBox(){
//Cell 1
echo '
<select name="Type" onchange="this.submit()">
<option value="1" >[All Card Types] </option>
<option value="Hero"' . ($_GET['Type'] == 'Hero' ? ' selected=""' : '') . '> Hero </option>
<option value="Event"' . ($_GET['Type'] == 'Event' ? ' selected=""' : '') . '> Event </option>
</select>
';
}
But for the sake of maintainability, you might do something more like this:
function searchBox() {
$types = array('Hero', 'Event');
$output = '<select name="Type" onchange="this.submit()">';
$output .= ' <option value="1" >[All Card Types] </option>';
foreach ($types as $type) {
$output .= '<option value="' . $type . '"' . ($_GET['Type'] == $type ? ' selected=""' : '') . '> ' . $type . ' </option>';
}
$output .= '</select>';
echo $output;
}
The problem is that you are outputting PHP as part of your literal text in the function. You need to rework the function so that the PHP logic is outside of the echo statement. In this case it may be easiest to jump in/out of PHP processing like this:
<?php function searchBox() { ?>
<select name="Type" onchange="this.submit()">
<option value="1" >[All Card Types] </option>
<option value="Hero" <?php if($_GET["Type"] == "Hero") { echo "selected=selected" ; }?>> Hero </option>
<option value="Event" <?php if($_GET["Type"] == "Event") { echo "selected=selected"; }?>> Event </option>
</select>
<?php } ?>
You are missing the quotes
echo "selected=\"selected\""
little messy but should work:
function searchBox(){
//Cell 1
echo '
<select name="Type" onchange="this.submit()">
<option value="1" >[All Card Types] </option>
<option value="Hero"';
if($_GET["Type"]=="Hero"){
echo "selected=selected";
}
echo '>Hero</option><option value="Event"';
if($_GET["Type"]=="Event"){
echo "selected=selected";
}
echo '>Event</option></select>';
}

Categories