Jump to content

XML Parser Loop Breaks


mcfmullen

Recommended Posts

I have an XML file (differences.xml)

<element>	
<item code="lM" name="castle" type="building">	
<cost>5000</cost>
</item>
...more items....
</element>

 

I have parser.php whih parses the XML and displays all item names in a checklist for the user to select items. These selected items are method=post back to parser.php which re-parses the XML where selected item = item name and SHOULD then display all selected items' names (and more):

<?php        
if (isset($_GET['formSubmit'])) {
$option = $_POST['option'];
$option = array_values($option);
if (!empty($option)){
	$xml = simplexml_load_file('differences.xml');
	$i = 0;
	$count = count($option);
	while($i < $count)
		{
			$selected = $xml->xpath("//item[@name='".$option[$i]."']");
			echo $selected[$i]['name'];
			$i++;
		}
}else{
	echo "No items selected, Please select:";
}
}else{

$xml = simplexml_load_file('differences.xml');
$object = $xml->xpath('//item');
$count = count($object);
$i = 0;
echo "<form method='POST' action='parser.php'>";
while($i < $count){
	$xmlname = $object[$i]['name'];
	echo "<input type='checkbox' name='option[".$i."]' value='$xmlname'>".$xmlname;
	echo "<br>";
	$i++;
}
echo "<br><input type='submit' name='formSubmit' value='Proceed'><br>";
echo "</form>";
}
?>

 

The problem is, the parser is only displaying the first selected item and isn't outputting the rest. To clarify the code:

- The first half executes only if items have been previously selected in the form. If no items were selected, it goes to the secod half, which parses the XML file and presents the checklist form for the user to select items. THIS WORKS GREAT.

- After pressing the submit button, the items are sent using POST and the page is reloaded. THIS WORKS GREAT.

- After reloading, the first half executes and finds the $_POST and resets the array so that it works with the coming loop. THIS WORKS GREAT.

- The loop parses the XML and returns only those item names that have been selected in the $_POST. THIS DOES NOT WORK.

 

So my question is: what's wrong with my code? The array $options has all the proper information, but the parser is only outputting the first item. All consecutive loops result in empty space with no output.

 

I've been at this for 2 weeks and can't find the solution. Any help is appreciated!

Link to comment
Share on other sites

I figured it out at last (thanks to no one's help!):

 

The problem was that

 

echo $selected[$i]['name'];

 

should have been:

 

echo $selected[0]['name'];

 

since the $selected Array is being reset everytime it loops, it only holds one item's info thus not needing to increase every time the loop runs!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.