Jump to content

next XML sibling


sKunKbad

Recommended Posts

I've got a simple XML file:

 

<?php
$quizxml = <<<XML
<?xml version='1.0' standalone='yes'?>
<quiz>
<questions>

	<question>
		<q>How many jelly beans can you eat?</q>
		<choice>1</choice>
		<response>You must be skinny</response>
		<choice>10</choice>
		<response>You must be normal</response>
		<choice>100</choice>
		<response>You must be big like me</response>
	</question>

	<question>
		<q>What is your favorite color?</q>
		<choice>Black</choice>
		<response>I like that one too!</response>
		<choice>Pink</choice>
		<response>Oh... thats nice:P</response>
		<choice>Chartreuse</choice>
		<response>Maybe I don't like you anymore.</response>
	</question>

	<question>
		<q>How do you feel right now?</q>
		<choice>Great</choice>
		<response>I feel great too!</response>
		<choice>Sick</choice>
		<response>Maybe tomorrow you will feel better.</response>
		<choice>Depressed</choice>
		<response>I'm so sorry.</response>
	</question>

</questions>
</quiz>
XML;
?>

 

and the goal is, when a post variable's value is determined to be a match,  I need to echo the following response sibling. I'm trying to use the following script with no luck. It's really late here, so don't laugh to hard ;D

 

$quiz = new SimpleXMLElement($quizxml);
foreach ($quiz->questions->question as $question) {
++$count;
$num = "Q$count";
$qnum = $_POST[$num];
echo "For question # $count, \"<em>" .$question->q. "</em>\", your answer was $qnum.<br />";
for ( $y=0; $y = count($question->choice); $y++ ){
	if ((string) $question->choice[$y] == $qnum) {
		echo $question->response[$y];
	}
}
echo '<br />';
}

 

Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/77601-next-xml-sibling/
Share on other sites

I'd format the question like this:

 

<question>
<q>How do you feel right now?</q>
<choices>
	<choice>
		<text>Great</text>
		<response>I feel great too!</response>
	</choice>
	<choice>
		<text>Sick</text>
		<response>Maybe tomorrow you will feel better.</response>
	</choice>
	<choice>
		<text>Depressed</text>
		<response>I'm so sorry.</response>
	</choice>
</choices>
</question>

(you might want to call it something else than "text" though, I just couldn't think of anything better).

 

This makes more sense to me and then you can just parse it "normally".

Link to comment
https://forums.phpfreaks.com/topic/77601-next-xml-sibling/#findComment-392811
Share on other sites

Hmm... I figured you'd ask that. I should perhaps have just bothered to explain it.

 

Something like this:

<?php
$quizxml = <<<EOF
<?xml version='1.0' standalone='yes'?>
<quiz>
<questions>
	<question>
		<q>How do you feel right now?</q>
		<choices>
			<choice>
				<text>Great</text>
				<response>I feel great too!</response>
			</choice>
			<choice>
				<text>Sick</text>
				<response>Maybe tomorrow you will feel better.</response>
			</choice>
			<choice>
				<text>Depressed</text>
				<response>I'm so sorry.</response>
			</choice>
		</choices>
	</question>
</questions>
</quiz>
EOF;

$quiz = new SimpleXMLElement($quizxml);
foreach($quiz->questions->question as $question)
{
echo "<h1>{$question->q}</h1>\n<ul>\n";
foreach($question->choices->choice as $choice)
{
	echo "\t<li>\n\t\t<strong>{$choice->text}</strong><br />\n\t\tResponse: {$choice->response}\n\t</li>\n";
}
echo "</ul>\n";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/77601-next-xml-sibling/#findComment-392991
Share on other sites

I formatted the XML differently like you suggested, and parsed the data normally, and everything is working great. Thank you for your help. I tend to work too much (like 16 hours yesterday), and near the end I get so exhausted I get stupid. Here was my code in the end:

 

The XML File:

<?php
$quizxml = <<<XML
<?xml version='1.0' standalone='yes'?>
<quiz>
<questions>

	<question>
		<q>How many jelly beans can you eat?</q>
		<option>
			<choice>1</choice>
			<response>You must be skinny</response>
		</option>
		<option>
			<choice>10</choice>
			<response>You must be normal</response>
		</option>
		<option>
			<choice>100</choice>
			<response>You must be big like me</response>
		</option>
	</question>

	<question>
		<q>What is your favorite color?</q>
		<option>
			<choice>Black</choice>
			<response>I like that one too!</response>
		</option>
		<option>
			<choice>Pink</choice>
			<response>Oh... thats nice:P</response>
		</option>
		<option>
			<choice>Chartreuse</choice>
			<response>Maybe I don't like you anymore.</response>
		</option>
	</question>

	<question>
		<q>How do you feel right now?</q>
			<option>
			<choice>Great</choice>
			<response>I feel great too!</response>
		</option>
		<option>
			<choice>Sick</choice>
			<response>Maybe tomorrow you will feel better.</response>
		</option>
		<option>
			<choice>Depressed</choice>
			<response>I'm so sorry.</response>
		</option>
	</question>

</questions>
</quiz>
XML;
?>

 

The PHP:

include 'quizxml.php';
$quiz = new SimpleXMLElement($quizxml);
foreach ($quiz->questions->question as $question) {
++$count;
$num = "Q$count";
$qnum = $_POST[$num];
echo "For question # $count, \"<em>" .$question->q. "</em>\", your answer was $qnum.<br />";
foreach ($question->option as $thisOption) {
	if ((string) $thisOption->choice == $qnum) {
		echo "<strong>$thisOption->response</strong><br />";
	}
}
echo '<br />';
}

Link to comment
https://forums.phpfreaks.com/topic/77601-next-xml-sibling/#findComment-393098
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.