Please check my code.
questions.php
<?php
include("db.php");
$dom = new DOMDocument("1.0");
$node = $dom->createElement("questions");
$parnode = $dom->appendChild($node);
$week = date("W");
$query = "SELECT * FROM ".QUIZ_QUESTION." WHERE week='".$week."'";
$result = mysql_query($query);
header("Content-type: application/xml; charset=ISO-8859-1");
while ($row = mysql_fetch_assoc($result)){
$node = $dom->createElement("question");
$question = $parnode->appendChild($node);
$ansAtrribute = $question->setAttribute("id",$row['id']);
$que = $dom->createElement("que",$row['question']);
$quenode = $question->appendChild($que);
$qryOpt = mysql_query("SELECT * FROM ".QUIZ_OPTION." WHERE question_id='".$row['id']."'");
while($gerOptions = mysql_fetch_array($qryOpt)){
$ans = $dom->createElement("ans",$gerOptions['option']);
$ansNode = $question->appendChild($ans);
$ansAtrribute = $ansNode->setAttribute("ans",$gerOptions['is_correct']);
}
}
echo $dom->saveXML();
?>
I am directly run the questions.php here is the XML output:
Output:
-<questions>
-<question id="1">
<que>What is PHP?</que>
<ans ans="0">Fruit</ans>
<ans ans="1">Language</ans>
<ans ans="0">Game</ans>
</question>
</questions>
quiz.php
<?php
$xmlDoc = new DOMDocument("1.0");
$xmlDoc->load("questions.php");
$questions = $xmlDoc->getElementsByTagName("question");
$question= $questions->item($qnum)->getElementsByTagName("que")->item(0)->nodeValue;
$ans1= $questions->item($qnum)->getElementsByTagName("ans")->item(0)->nodeValue;
$ans1r= $questions->item($qnum)->getElementsByTagName("ans")->item(0)->getAttribute('ans');
$ans2= $questions->item($qnum)->getElementsByTagName("ans")->item(1)->nodeValue;
$ans2r= $questions->item($qnum)->getElementsByTagName("ans")->item(1)->getAttribute('ans');
$ans3= $questions->item($qnum)->getElementsByTagName("ans")->item(2)->nodeValue;
$ans3r= $questions->item($qnum)->getElementsByTagName("ans")->item(2)->getAttribute('ans');
?>
I am running the quiz.php but the questions.php doesn't load.
I am getting following error:
Warning: DOMDocument::load() [domdocument.load]: Start tag expected, '<' not found in file:///G:/xampp/htdocs/txtweb/quiz/questions.php, line: 30 in G:\xampp\htdocs\txtweb\quiz\quiz.php on line 3
Fatal error: Call to a member function getElementsByTagName() on a non-object in G:\xampp\htdocs\txtweb\quiz\quiz.php on line 6
I am posting my complete code. Please let me know where is error of my code?
load(question.php") doesn't load the response given by the web server, but instead loads the actual source filequestions.php, hence you have no starting<`question.phpis showing the xml format. Please check my outputquestions.xmlinstead ofquestions.phpi am getting the correct output. But how can i use it in withphpextension. This is dynamic content.