Jump to content

where XML attribute = PHP $variable


mcfmullen

Recommended Posts

I have a parser.php file that presents me with a list of items inside an XML file

 

differences.xml:

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

 

After selecting wanted items, the form submits to parsed.php. Now I want parsed.php to go back into the same XML file and insert only those selected items into a new array.

 

parser.php code:

<?php
if (empty($option)){
$xml = simplexml_load_file('differences.xml');
$object = $xml->xpath('//item');
$count = count($object);
$i = 0;
echo "<form method='post' action='parsed.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>";
}
?>

 

parsed.php code:

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

 

The problem I am having is that

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

is not returning any values (it is empty, I checked). The thing is though that

$option[$i]

does contain the selected value. I have narrowed my problem down to this part of my code:

 

$object = $xml->xpath('//item[@name="$option[$i]"]');
echo $object[$i]['name'];

 

Can someone help me narrow down my problem?

Link to comment
Share on other sites

change:

$object = $xml->xpath('//item[@name="$option[$i]"]');

to:

$object = $xml->xpath("//item[@name=\"$option[$i]\"]");

or

$object = $xml->xpath('//item[@name="'.$option[$i].'"]');

because php does not parse single-quote strings. PHP only parses double-quoted strings.

 

Link to comment
Share on other sites

Your code works... BUT

 

I'm having a problem getting my loop to work. I know it works because if I echo $i, I get 0,1,2,etc.. depending on how many items I have selected in my form. The problem is when I try to get the call to look in the xml while looping.

 

The xml is only outputting the first item $option[$i]['name'] and giving blanks for the rest. Why is this?

 

<?php        
if (isset($_GET['formSubmit'])) {
$option = $_GET['option'];
$option = array_values($option);
if (!empty($option)){
	$xml = simplexml_load_file('differences.xml');
	$i = 0;
	$count = count($option);
	while($i < $count)
		{
			$option = $xml->xpath("//item[@name='".$option[$i]."']");
			echo $option[$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='GET' 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 code works by checking if the form was sent. If yes, $_GET the form and store the information in a new array while resetting the array (so it works with the $i in the loop). Then echo out the XML name for each item in the array. If the form was sent blank, echo message to select items and present the form. If no form was sent to begin with, present the form.

 

For testing purposes, I'm just trying to echo out the same information the form is presenting, only without being inside the form. The plan is to xpand this to echo the full xml info after the form is passed.

 

Your help is appreciated, thanks.

Link to comment
Share on other sites

The code still does not work. It shows me only the first selected item and nothing else.

 

if (isset($_GET['formSubmit'])) {
$option = $_GET['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:";
}

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.