XML + PHP help needed. Also POST help.
Posted: Thu Oct 21, 2010 4:04 pm
Hi. First off I'd like to thank anyone who can help me out here.
I'm still new to PHP, so my knowledge is still quite limited and I find myself referencing w3schools quite often.
Here is what I need to achieve, but I have no idea about how to do this:
Basically, the site I'm building contains a quiz. Each question is timed individually with a simple JS onLoad timer function. Every time the users submits (POST) the results, the page reload, therefore the timer reloads. This is all fine and dandy since they either got the question right and advance or got it wrong and a new same level problem is generated. I have an idea of how to store the timer value from the original question so that it gets echo'd onto the page after page reload. But this is not sufficient for me. As I need a way to store the timer for each attempt on every question.
I had the idea of writing them all to XML and then parsing the XML into HTML output based on this type of structure:
This method to me seems very sloppy and inelegant. I need some idea's of other methods that I can use here to achieve the same effect.
The PHP code I've got written so far is:
The issue I have is how to increase the number of attempts after every submit press (page reload) unless the answer was correct, in which case it goes back to 0.
Is there a way to query the XML doc to get the amount of fields under <question 1>?
Also, I'm lost as to how to update the XML doc rather then just creating a new one.
Any help here would be greatly appreciated. I was literally kept awake all of last night contemplating this issue.
I'm still new to PHP, so my knowledge is still quite limited and I find myself referencing w3schools quite often.
Here is what I need to achieve, but I have no idea about how to do this:
Basically, the site I'm building contains a quiz. Each question is timed individually with a simple JS onLoad timer function. Every time the users submits (POST) the results, the page reload, therefore the timer reloads. This is all fine and dandy since they either got the question right and advance or got it wrong and a new same level problem is generated. I have an idea of how to store the timer value from the original question so that it gets echo'd onto the page after page reload. But this is not sufficient for me. As I need a way to store the timer for each attempt on every question.
I had the idea of writing them all to XML and then parsing the XML into HTML output based on this type of structure:
Code: Select all
<quiz 1>
<question 1>
<attempt 1>
RESULT
</attempt 1>
<attempt 1>
RESULT
</attempt 1>
</question 1>
</quiz 1>
The PHP code I've got written so far is:
Code: Select all
function createXML(){
global $quiznumberofattempts, $questionnumberofattempts, $myusername;
$domDoc = new DOMDocument;
$rootElt = $domDoc->createElement('root');
$rootNode = $domDoc->appendChild($rootElt);
$subElt = $domDoc->createElement('foo');
$attr = $domDoc->createAttribute('ah');
$attrVal = $domDoc->createTextNode('OK');
$attr->appendChild($attrVal);
$subElt->appendChild($attr);
$subNode = $rootNode->appendChild($subElt);
$textNode = $domDoc->createTextNode('Wow, it works!');
$subNode->appendChild($textNode);
$domDoc->save('users/' . $myusername . '/quizresults' . $quiznumberofattempts . '.xml');
}
if(!file_exists('users/')){
mkdir('users/');
if (!file_exists('users/' . $myusername)){
mkdir('users/' . $myusername);
}
}
if(!$_POST['submit']){
if(!file_exists('users/' . $myusername . '/quizresults' . $quiznumberofattempts . '.xml')){
createXML();
}
}
Is there a way to query the XML doc to get the amount of fields under <question 1>?
Also, I'm lost as to how to update the XML doc rather then just creating a new one.
Any help here would be greatly appreciated. I was literally kept awake all of last night contemplating this issue.