2012-07-03 7 views
-2

XML 시트의 데이터가 PHP 배열로 변환되고 아래 코드로 퀴즈 형식 질문을 만들려고했지만 작동하지 않습니다. 확실하지 만약 내가 잘못코드를 객관식 퀴즈 형식으로 변환

<?php 
function get_answer($input){ 
if ($input == ($att['ans'])) 
{echo 
"Correct Answer . {$att['ans_desc']}"; 
} 
else { 
echo "Wrong Answer . {$att['ans_desc']}"; 
} 
} 
$xml = <<<XML 
<dataroot> 
<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><![CDATA[a]]></ans> 
<ans_desc><![CDATA[<FONT FACE="Arial">kilogram is the name of the fundamental unit of mass</FONT>]]></ans_desc> 
</content> 
</dataroot> 
XML; 
$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 /> . 
</form>"; 
?> 
+2

"작동하지 않는다"는 것이 도움이되지 않습니다. 작동하지 않는 것은 무엇입니까? 어떤 기능이 누락 되었습니까? – Amaerth

+1

AJAX 호출없이 PHP를 클라이언트 측에서 효과적으로 실행하려고합니다. 그리고 그것은 단지 이런 식으로 작동하지 않습니다. 대신 (아마도) 서버에 응답을 제출하고 JavaScript로 응답을 구문 분석해야합니다. – raina77ow

답변

0

저를 수정, 당신이 달성하려고하는 ... 여러 질문에 대한 작업을 얻는 방법 : 사용자가 답변을 클릭하여 질문에 대답 할 때, 대답이 확인됩니다 즉시 결과가 신속하게 나타납니다.

그렇다면 거기에있는 항목이 작동하지 않습니다. 나는 그것을하는 방법을 알고 있지만, 그것이 당신이 원하는 것이라면 명확한 설명이 필요합니다.

또한 코드는 오류가 많아서 코드가 작동하지 않습니다.

  • html의 onclick 속성은 javascript 함수를 실행하고 PHP 함수는 이런 방식으로 실행되지 않습니다.
  • 동일한 태그에 여러 개의 유사한 ID를 할당 할 수 없습니다.
  • 질문을 인쇄하는 코드는 foreach 루프에 있지 않습니다. 그래서 "no such index"와 같은 오류가 발생한다고 가정합니다.