XML + PHP help needed. Also POST help.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

XML + PHP help needed. Also POST help.

Post by spedula »

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:

Code: Select all


<quiz 1>
   <question 1>
      <attempt 1>
         RESULT
      </attempt 1>
      <attempt 1>
         RESULT
      </attempt 1>
   </question 1>
</quiz 1>

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:

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();
	}
}
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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: XML + PHP help needed. Also POST help.

Post by requinix »

A database is more appropriate for this. Try MySQL.

You store questions in one place and everybody's answers (and time taken) in another place. SQL will let you insert, edit, and retrieve information very easily - much more easily than with XML.
Post Reply