I need to select value dynamically using PHP in drop down list but in my case its not working as expected. I am explaining my code below.
<?php
$citySelected = "";
if(!isset($_GET['city'])){
$citySelected = 'selected="selected"';
}
?>
<select id="selectedLoc" name="selectedLoc" class="chosen-select form-control">
<option value="">Select City</option>
<option value="0" <?php echo $citySelected; ?>>Global</option>
<?php
foreach ($locationArr as $key => $value) {
$id = $value['id'];
$city = $value['city'];
$location = $value['location'];
$selected = "";
if(isset($_GET['city']) && $_GET['city']== $value['id']) {
$selected ='selected="selected"';
}
echo "<option value='$id' $selected>$location</option>";
}
?>
</select>
Here I need when there is any query string value then it will match with respective id and select that option and if there is no query string value at all then the global option will select.
Inside if condition ';' is missing. It should be like
if(isset($_GET['city']) && $_GET['city']== $value['id']){echo ' selected="selected"';}else{echo '';}
You can also use conditional operator for code simplicity, eg:-
<?php echo (isset($_GET['city']) && $_GET['city']== $value['id'])? ' selected="selected"':''; ?>
I have a PHP select that dynamically populates based on a MySQL recordset and uses a in_array value to identify and select the value(s) found in the database like this:
<select name="StaffSetSelection[]" size="5" multiple="MULTIPLE" id="StaffSetSelection">
<option <?php if ($totalRows_StaffSetID == 0) { echo "selected"; } ?> value="">Choose a Staff Set</option>
<?php
do {
?>
<option <?php if (in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
echo "selected";
} ?> value="<?php echo $row_StaffSetChoices['EmpNumber'] ?>"><?php echo $row_StaffSetChoices['EmpFirstName'] ?></option><?php
} while ($row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices));
$rows = mysql_num_rows($StaffSetChoices);
if ($rows > 0) {
mysql_data_seek($StaffSetChoices, 0);
$row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices);
}
?>
</select>
Instead of displaying the selected values in a form option select I'd like to just display them as text.
I've tried to use the in_array again in a simple php echo but it does not display the data and there are no errors in my apache log.
This is what I've tried:
<?php if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) echo $row_StaffSetChoices['EmpFirstName']?>
I think you forgot the brackets,
Try this way:
<?php
if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
echo $row_StaffSetChoices['EmpFirstName'];
}
?>
<?php
$tab=$_GET['liste'];
$max=sizeof($tab);
echo$max;
echo$_GET['type'];
if ($_GET['type']='multiple')
{
?>
<form name='form'>
<select size=<?php $max ?> multiple>
<?php
for($i=0;$i<$max;$i++){
echo"<option value=$i>$tab[$i]</option>";
}
echo"</select>";
echo"</form>";
}
else{
?>
<form name='form'>
<select size=<?php $max ?>>
<?php
for($i=0;$i<$max;$i++){
echo"<option value=$i>$tab[$i]</option>";
}
echo"</select>";
echo"</form>";
}
?>
in two cases it's showing just the dropdown list width single choice even if $_GET['type']='multiple' .
I tried
<select size=<?php $max ?> <?php echo$_GET['type']; ?>>
instead of
<select size=<?php $max ?> multiple>
But still not working !!
Any suggestions please !
Change
<select name='ville' size="$max" multiple>
to
<select name='ville' size="<?=$max?>" multiple>
There is a lot that needs fixing here (no offense), so let's start with
if($_GET['type']='multiple')
All this is doing is assigning the value 'multiple' the $_GET['type'] variable, so this will always hit. Next, as mentioned before, you need to change
<?php $max ?>
as it currently does nothing. Next up is
echo"<option value=$i>$tab[$i]</option>";
When using an array element in a string, must be wrapped with { and }
Let's take a look at some cleaned up code because there are too many errors here to pinpoint the real issue:
<?php
$tab = $_GET['liste'];
$max = sizeof($tab);
#echo $max;
#echo $_GET['type'];
$multiple = '';
if($_GET['type'] == 'multiple')
{
$multiple = ' multiple="multiple"';
}
?>
<form name='form'>
<select size="<?php echo $max ?>" <?php echo $multiple; ?>>
<?php
foreach($tab as $key => $value)
echo "<option value='{$key}'>{$value}</option>";
?>
</select>
</form>
Edit: Just verified - variables inside a string must always be wrapped in { and } if it's referencing an array element (this does not apply to objects after all):
$a = array('foo' => 'bar');
$o = new stdclass;
$o->foo = 'bar';
$o->baz = array('foo' => 'bar');
echo "$a['foo']"; // Syntax error
echo "$o->foo"; // echos "bar"
echo "{$a['foo']}"; // echos "bar"
echo "$o->baz['foo']"; // Syntax error
In general, it's a Good Idea to always wrap variables in strings with { and }, even if it's legally allowed:
echo "Something {$here}"; // Obvious variable is obvious
i am selecting an entry from the database to be edited on php/html. i can call the values of a table from the database and put it in a multiple tag.
$job_exp_tags = $row[16];//value from another table
$job_tags = explode(',',$job_exp_tags);//array to be put "selected" on the multiple select tag
$sql_2 = "SELECT _id, job_name from job_tags";
$sel_2 = mysql_query($sql_2);
$array = array();
while($row_new = mysql_fetch_assoc($sel_2)){
$array[] = $row_new;
}
<tr>
<td>Job Experience:<br>
(Hold ctrl to select multiple)</td>
<td><select name = 'job_tags[]' multiple>
<?php
foreach($array as $value){ ?>
<option value ='<?php echo $value['_id']; ?>'> <?php echo $value['job_name']; ?> </option>
<?php
}
?>
</select> </td>
</tr>
my problem is how can i put selected values to it from the explode statement
edit:
i've solved the problem, thanks anyway guys!
<option value ="<?php echo $value['_id']; ?>" <?php echo in_array($value['_id'], $job_tags) ? 'selected="true"' : null; ?>><?php echo $value['job_name']; ?></option>
Just check if its in your array, if so, set it selected:
foreach($array as $value){ ?>
$selected = in_array($value, $job_tags) ? ' selected ' : '';
/* Or [selected="selected"] if you dont use html5 yet (which you should) */
<option value ='<?php echo $value['_id']; ?>' <?php echo $selected; ?>> <?php echo $value['job_name']; ?> </option>
<?php
}
Your code can be simplified though:
foreach($array as $value){
$selected = in_array($value, $job_tags) ? ' selected="selected" ' : '';
?>
<option value="<?=$value['_id']?>" <?=$selected?> > <?=$value['job_name']?> </option>
<?php
}
I changed the quotes arround the value to doubles, not really a rule, but it is a good practice to do so. The other change is the short echo. Small demo, both do the same:
<php $var = 'foorbar'; ?> <!-- A bit weird, but this is demo-purpose -->
<span><?php echo $var; ?></span>
<span><?=$var?></span>
You could look for the value in the string $job_exp_tags and if found set the selected property.
foreach($array as $value){
$selected = strpos($job_exp_tags, $value) ? 'selected' : '';
echo '<option value="'.$value['_id'].'" '.$selected.'>'.$value['job_name'].'</option>';
}
I currently have a form built in which after validation, if errors exist, the data stays on screen for the consumer to correct. An example of how this works for say the 'Year of Birth' is:
<select name="DOB3">
<option value="">Year</option>
<?php
for ($i=date('Y'); $i>=1900; $i--)
{
echo "<option value='$i'";
if ($fields["DOB3"] == $i)
echo " selected";
echo ">$i</option>";
}
?>
</select>
If an error is found, the year of birth value returns the year previously entered. I am able to have this work on all field with the exception of my 'State' field. I build the array and function for the drop down with the following code:
<?php
$states_arr = array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"District Of Columbia",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming");
function showOptionsDrop($array, $active, $echo=true){
$string = '';
foreach($array as $k => $v){
$s = ($active == $k)? ' selected="selected"' : '';
$string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";
}
if($echo) { echo $string;}
else { return $string;}
}
?>
I then call the function from within the form using:
<td><select name="State"><option value="">Choose a State</option><?php showOptionsDrop($states_arr, null, true); ?></select></td>
Not sure what I'm missing but would love any assistance if somebody sees the error in my code.
Thanks!
Have a look at the code:
<?php showOptionsDrop($states_arr, null, true); ?>
You are passing null, so $active will always be null. The condition
($active == $k)
will never we evaluate to true.
You should pass the value you get from the form instead, e.g.:
<?php showOptionsDrop($states_arr, isset($fields['State']) ? $fields['State'] : null, true); ?>
You really should try to separate the PHP from the HTML, especially in your first example
Update:
Actually it is not that much, but consider to use the alternative syntax for control structures:
<select name="DOB3">
<optgroup label="Year">
<?php for ($i=date('Y'); $i>=1900; $i--) : ?>
<option value="<?php echo $i ?>"
<?php echo ($fields["DOB3"] == $i) ? 'selected="selected"' : '' ?> >
<?php echo $i ?>
</option>
<?php endforeach; ?>
</optgroup>
</select>
Also if you want to give the options some kind of label, you can do this with the optgroup HTML tag (this is not selectable).