Initially select option for a dropdown menu in php

What I am trying to do is to alter which option for a drop-down to be selected.

I’m working on a page to modify entries into a database and I have a few entries I like to keep locked down to a pre-determined options. I can grab the value that it’s supposed to be from the database and I want that value to be selected from the drop down menu.

Is there a way to do this other than using an if statement or case statement to create html code to set the selected value?

echo "
<li>	
<label>Question:</label>
 <select name='question' id='question'>
";

if ($q == 'Yes') {echo "<option value='Yes'  selected=selected> Yes</option>";} else {echo "<option value='Yes'>Yes</option>";}	
if ($q == 'No') {echo "<option value='No' selected=selected>No</option>";} else {echo "<option value='No'>No</option>";}				

echo "
</select></li>
";

What I want to try to do is something more on the lines of:

echo "
<li>	
<label>Question:</label>
 <select name='question' id='question' value=$q>
    <option value='Yes'>Yes</option>	
    <option value='No'>No</option></select></li>
";

Not possible. HTML requires the default <option> to have the “selected” attribute. <select> lists don’t have a value attribute.

Not sure if this is what you mean (I’m guessing that you have a lot of values and don’t want to write a lot of if statements or cases), but, if you’re pulling all of the options from the database (or store them in an array) you could run through them using foreach and compare with the default value that you want…


$options = array('Yes','No');
$default = "No";

echo "
<li>	
<label>Question:</label>
 <select name='question' id='question'>
";

foreach($options as $option) {
  $selected = ($option == $default) ? ' selected="selected"' : '';
  echo "<option value=\\"$option\\"$selected>$option</option>";
}

echo "
</select></li>
";

The result should be as below


// $default == 'Yes'
<option value="Yes" selected="selected">Yes</option>
<option value="No">No</option>

// $default == 'No'
<option value="Yes">Yes</option>
<option value="No" selected="selected">No</option>

Sorry if this isn’t what you were looking for, late and tired :slight_smile:

Note: untested
Edit: removed pointless statement, I said I was tired…

By locked down do you mean just pre-selected or pre-selected and locked so that the user sees the selection but is unable to control it?

Perhaps something like this would work for you. Not to sure without more details though.


<?php
$arrConfig = array(
	array(
		'selected'=>'4'
		,'locked'=>true
		,'values'=>array('','1','2','3','4','5','6')
	)
	,array(
		'selected'=>'b'
		,'locked'=>false
		,'values'=>array('','a','b','c','d','f','g')
	)
	,array(
		'selected'=>''
		,'locked'=>false
		,'values'=>array('','1a','2b','3c','4d','5d')
	)
);

foreach($arrConfig as $arrOption) {
	
	printf(
		'<select&#37;s>'
		,$arrOption['locked'] === true?' disabled="disabled"':''
	);
	
	foreach($arrOption['values'] as $mixValue) {
		printf(
			'<option value="%s"%s>%s</option>'
			,$mixValue 
			,$mixValue == $arrOption['selected']?' selected="selected"':''
			,strlen($mixValue) == 0?'--':$mixValue
		);
	}
	echo '</select>';
}
?>