Data from a XML sheet is converted to php array and I have tried to create a quiz format question with the below code, but it does't work. Not sure how to get it working for multiple questions...

function get_answer($input){
if ($input == ($att['ans']) )
"Correct Answer . {$att['ans_desc']}";
else {
echo "Wrong Answer . {$att['ans_desc']}";
$xml = <<<XML
<content id= "1">
<quest><![CDATA[<FONT FACE="Arial">Which of the following is not the measure of a    physical quantity?</FONT>]]></quest>
<opt1><![CDATA[<FONT FACE="Arial">kilogram</FONT>]]></opt1>
<opt2><![CDATA[<FONT FACE="Arial">impulse</FONT>]]></opt2>
<opt3><![CDATA[<FONT FACE="Arial">energy</FONT>]]></opt3>
<opt4><![CDATA[<FONT FACE="Arial">density</FONT>]]></opt4>
<ans_desc><![CDATA[<FONT FACE="Arial">kilogram is the name of the fundamental unit of mass</FONT>]]></ans_desc>
$data = new SimpleXMLElement($xml,LIBXML_NOCDATA);
$array = json_decode(json_encode($data),true);
foreach($array as $content => $att){
echo $att['quest'];
echo "<br />";
echo "<form name='quiz'> .
<input type=\"radio\" id='options' onclick = \"getAnswer({$att['ans']})\" />. {$att['opt1']} . <br />" .
"<input type=\"radio\" id='options' onclick = \"getAnswer({$att['ans']})\"/>. {$att['opt2']} . <br />" .
"<input type=\"radio\" id='options' onclick = \"getAnswer({$att['ans']})\"/>. {$att['opt3']} . <br />" .
"<input type=\"radio\" id='options' onclick = \"getAnswer({$att['ans']})\"/>. {$att['opt4']} . <br /> .

Recommended Answers

All 2 Replies

First, please lose all those <font> tags. They've been mostly obsolete for at least ten years.

In the question form, add a hidden form field that includes the question id. That's the only way you'll know which question to test against.

Give each radio option a value attribute that identifies which option the user is selecting, ie value="a".

echo "<form name='quiz'>
<input type=\"hidden\" name=\"question\" value=\"{$att['@attributes']['id']}\">
<label><input type=\"radio\" name='options' id='options' value=\"a\" onclick = \"getAnswer({$att['ans']})\" /> {$att['opt1']}  </label><br />" .
"<label><input type=\"radio\" name='options' id='options' value=\"b\" onclick = \"getAnswer({$att['ans']})\"/> {$att['opt2']}  </label><br />" .
"<label><input type=\"radio\" name='options' id='options' value=\"c\" onclick = \"getAnswer({$att['ans']})\"/> {$att['opt3']}  </label><br />" .
"<label><input type=\"radio\" name='options' id='options' value=\"d\" onclick = \"getAnswer({$att['ans']})\"/> {$att['opt4']}  </label><br />
<input type=\"submit\" value=\"GO\">

Now you have a form that contains the elements you need to test the quiz response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.