Hi
I use the following script to populate a drop down menu from a table:
roleid role
1 classA
2 classB
3 classC
<select name="role">
<?php
$query = "select roleid, role from role ";
$result = mysql_query($query) or die('Error : ' . mysql_error());
while($arrayRow = mysql_fetch_assoc($result)) {
$intIdField = $arrayRow['roleid'];
$strNameField = $arrayRow['role'];
echo "<option value=\"$intIdField\">$strNameField</option>\n";
}
echo "</select>\n\n";
?>
The roleid can be input to table 'Score' which has a reference roleid
scoreid student score roleid
1 Tom 92 2
2 Allen 87 3
3 Edith 89 1
So, I know Tom got 92 mark from classB.
Now, I have a score edit page needs to retrieve score data.
student - it can display the DB data, e.g. it shows Tom when I select scoreid = 1
score - same as student
roleid - How can it be presented as Radio Group with checked value for DB value?
If I select scoreid = 1, roleid = 2
I want the radio group shows:
classA unchecked
classB checked
classC unchecked
I tried this code but failed to show checked value of classB:
<?php
$query = "select roleid, role from role ";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$sid = (isset($_POST['role']) ? $_POST['role'] : '');
while($row = mysql_fetch_assoc($result))
{
echo '<input type="radio" name="role" value="' . $row['roleid']
. '"' . ($sid == $row['roleid'] ? 'checked="checked"' : '') . '>'
. $row['role'] . "<br>\n";
}
?>
It can show the radio group, but there is no checked value.
How can I do it?
Secondly, if I want the checked value to be shown in drop down menu, how can I do it too?
Thanks for help