Derek Derek - 7 months ago 7
SQL Question

PHP Syntax for webpage radio buttons

Currently I have the following table in my database

|AnswerID|QuestionID|AnswerText|
|21|2|User|
|22|2|Admin|
|23|2|Guest|
|24|2|User2|


and written the following php script

$questionID=0;
while ($list = mysql_fetch_assoc($result))
{
echo $list['QuestionID'] . ":" . $list['QuestionText'] . "<br/>";
$questionID=$list['QuestionID'];
}
$optionsquery="SELECT AnswerText,AnswerID FROM Options Where QuestionID=".$questionID;
$optionsresult=mysql_query($optionsquery) or die ('Query failed:'. mysql_error());
while($row = mysql_fetch_array($optionsresult))
{
'Display the multiple choices
associating the AnswerID with the appropriate AnswerText.'

E.g. echo "<input type='radio' name='QuestionID'
value='AnswerID'/>"
corresponding AnswerText given the AnswerID;

E.g for the first radio button:

echo "<input type='radio' name='2' value='21'/> User;

}


Could I get help with the PHP syntax for the radio buttons?

Answer

You could do:

echo "<input type='radio' id='radio-" . $row["QuestionID"] . "-" . $row["AnswerID"] . "' name='" . $row["QuestionID"] . "' value='" . $row["AnswerID"] . "' /> <label for='radio-" . $row["QuestionID"] . "-" . $row["AnswerID"] . "'>" . $row["AnswerText"] ."</label><br/>";