mcfmullen Posted December 27, 2010 Share Posted December 27, 2010 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 https://forums.phpfreaks.com/topic/222707-where-xml-attribute-php-variable/ Share on other sites More sharing options...
Unirawan Posted December 27, 2010 Share Posted December 27, 2010 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 https://forums.phpfreaks.com/topic/222707-where-xml-attribute-php-variable/#findComment-1151802 Share on other sites More sharing options...
mcfmullen Posted December 31, 2010 Author Share Posted December 31, 2010 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 https://forums.phpfreaks.com/topic/222707-where-xml-attribute-php-variable/#findComment-1153382 Share on other sites More sharing options...
requinix Posted December 31, 2010 Share Posted December 31, 2010 It doesn't work because you overwrite $option. $option = $xml->xpath("//item[@name='".$option[$i]."']"); Link to comment https://forums.phpfreaks.com/topic/222707-where-xml-attribute-php-variable/#findComment-1153433 Share on other sites More sharing options...
mcfmullen Posted January 2, 2011 Author Share Posted January 2, 2011 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 https://forums.phpfreaks.com/topic/222707-where-xml-attribute-php-variable/#findComment-1153916 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.