View Full Version : Select Option and Database Query

08-21-2007, 10:56 AM
I am trying to write a function for an update script I am doing.

Currently, there is a table called Category which contains a numeric value of 1-3. The Category is displayed as text output thanks to an array and or if statement.

If Category = 1, then Output = Test 1
If Category = 2, then Output = Test 2
If Category = 3, then Output = Test 3

Here is what my code currently looks like:

<td><select name='Category'>
<? $s_array = array('Test 1','Test 2','Test 3')?>
<option SELECT value='<? if(array_key_exists($onenav->Category, $s_array)) {?><?php echo($onenav->Category);?>'><? echo $s_array[$onenav->Category] ;?><? } ?></option>
<option value='1'> Test 1 </option>
<option value='2'> Test 2 </option>
<option value='3'> Test 3 </option>
</select> <br />

You will notice that the array exist and SHOULD list the Category name if the number matches the array, however, it does not. Also, you will notice that the Test 1, Test 2 and Test 3 are listed as select <option> tags. This is because when a user is updating the page, if the current Category is Test 1, and he wishes to update to Test 2, then he can. However, I would like it so that NO Category is repeated twice. If there anything I can do to allow me to have the option selected if it exist? and also, how do I get that array working to list the Category as Text output selecting the option that matches the current database value.

08-21-2007, 02:57 PM
I'm afraid I found the description of the problem a little confusing but I think this is what you're after:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<td><select name='Category'>
<option value="-1">--Select--</option>
$s_array = array(1=>'Test 1','Test 2','Test 3');
foreach($s_array as $value=>$text){
echo '<option value="'.$value.'"';
if(array_key_exists(@$onenav->Category, $s_array)){
echo ' selected';
echo '>'.$text.'</option>';
</select> <br />

If not, or you need any explanations let me know.