tony_p Posted May 18, 2013 Share Posted May 18, 2013 XPATH method for retuning multiple dynamic results from a XML file using PHPI have a form that the user types or selects a value the form is as follows<form id="search_photos" action="photo_result.php" method="get"> <input value="" name="photographer_id" id="photographer_id" style=" border:1px solid Silver;" type="text"> <select name="Photographer" id="Photographer" style="height:23px; border:1px solid Silver;"> <option selected="selected" value="x">Any Photographer</option> <option value="John">John</option> <option value="Fred">Fred</option> <option value="Joseph">Joseph</option> </select> <select name="images" id="images" style="height:23px; border:1px solid Silver;"> <option selected="selected" value="x">All Images</option> <option value="0">None</option> <option value="a">Image a</option> <option value="b">Image b </option> <option value="c">Image c </option> </select> <input name="Submit" value="Search Now >" id="Submit" class="Adv1_Filter_Button" type="submit"></form>Then the search_photo.php script that catches the result of the form and filters the values entered by the user asfollows<?php $xml = simplexml_load_file("photo.xml"); for ($i = 0; $i < count($xml); $i++){ if(isset($_GET["LocationName"])) { $photographer_id = $_GET["LocationName"]; } $result = $xml->xpath('/root/area[photographer_id=' . $photographer_id . '] '); } if(isset($_GET["Photographer"])) { $photographer = $_GET["Photographer"]; } $result = $xml->xpath('/root/area[photographer_id=' . $photographer_id . '] '); if(isset($_GET["images"])) { $image = $_GET["images"]; } echo $photographer_id; echo $photographer; echo $image; var_dump ($result); ?>The $result from the first XPATH pass is correct if all that is set is ‘photographer_id’ if I then try $result=$xml->xpath ('/root/area[photographer_id=' . $photographer_id . '] | /root/area[photographer=' . $photographer .']'); and select 1 and fred then I get the result of an array of all four when it should be an empty array I have trie & (and) insted of | (or) but just returns an empty array should I convert the $result to an object or is the a way of saving the $result to an xml file Quote Link to comment Share on other sites More sharing options...
tony_p Posted May 19, 2013 Author Share Posted May 19, 2013 Sorry I forgot to add the XML content <?xml version="1.0" encoding="UTF-8"?><root> <area> <photographer_id>1</photographer_id> <photographer>John</photographer> <image>a</image> </area> <area> <photographer_id>1</photographer_id> <photographer>John</photographer> <image>b</image> </area> <area> <photographer_id>1</photographer_id> <photographer>John</photographer> <image>c</image> </area> <area> <photographer_id>2</photographer_id> <photographer>Fred</photographer> <image>a</image> </area></root> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.