This code below works but it requires a mysql table to have the data to list in the selection box. See how it works then view the second code which does not work.
$result = mysql_query('SELECT field FROM table');
// get list of fields to place in option selection box
while($row = mysql_fetch_array($result))
{
$result2 = mysql_query('SELECT field2 FROM table2 WHERE id="'.$_GET['id'].'"');
// get the selected field(2) from the result with matching id
while($row2 = mysql_fetch_array($result2))
{
// if the selected field(2) matches field in list make it selected
if($row2['field2'] == "{$row['field']}")
{
$selected = 'selected';
}else{
$selected = '';
}
echo '<option '.$selected.' value="'.$row['field'].'">'.$row['field'].'</option>';
}
}
}
Now the second code.
$result = mysql_query('SELECT field FROM table WHERE id="'.$_GET['id'].'"');
// get the selected field from the result with matching id
while($row = mysql_fetch_array($result))
{
// if the selected field matches my option in list make it selected
if($row['field'] == 'my_option1')
{
$selected = 'selected';
}
elseif($row['field'] == 'my_option2')
{
$selected = 'selected';
}else{
$selected = '';
}
echo '<option '.$selected.' value="my_option1">My Option One</option>';
echo '<option '.$selected.' value="my_option2">My Option Two</option>';
}
To the best of my knowledge it should work but it doesn't therefore I am wrong so can someone please help me. Should I be using a switch thingo instead? 😕
I mean the first way is best for 10 results but if you only have 2 it seems too much work to have a mysql table for 2 results :bemused: