XPATH method for retuning multiple dynamic results from a XML file using PHP -
i have form user types or selects value form follows
<form id="search_photos" action="photo_result.php" method="get"> <select name="photographer_id" id="photographer_id" style="height:23px; border:1px solid silver;"> <option selected="selected" value="x">any photographer_id</option> <option value="john">john</option> <option value="fred">fred</option> <option value="joseph">joseph</option> </select> <select name="photographer_id" id="photographer_id" style="height:23px; border:1px solid silver;"> <option selected="selected" value="x">any photographer_id</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</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> <select name="images_id" id="images_id" style="height:23px; border:1px solid silver;"> <option selected="selected" value="x">all images</option> <option value="0">none</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <input name="submit" value="search >" id="submit" class="adv1_filter_button" type="submit">
then search_photo.php
script catches result of form , filters values entered user follows
$xml = simplexml_load_file("photo.xml"); ($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
first xpath pass correct if set ‘photographer_id’ if try $result = $xml->xpath('/root/area[photographer_id="' . $photographer_id . '"] | /root/area[photographer="' . $photographer . '"]');
, select 1
, fred
result of array of 4 when should empty array can advise how correct error. sorry michi here xml file
<?xml version="1.0" encoding="utf-8"?> <root> <area> <photographer_id>1</photographer_id> <photographer>john</photographer> <image>a</image> <image_id>1</image_id> </area> <area> <photographer_id>1</photographer_id> <photographer>john</photographer> <image>b</image> <image_id>2</image_id> </area> <area> <photographer_id>1</photographer_id> <photographer>john</photographer> <image>c</image> <image_id>3</image_id> </area> <area> <photographer_id>2</photographer_id> <photographer>fred</photographer> <image>a</image> <image_id>4</image_id> </area> <area> <photographer_id>3</photographer_id> <photographer>joseph</photographer> <image>a</image> <image_id>5</image_id> </area> </root>
does help, final xml file larger though.
hi had simillar problem long in script, have taken idea there , came hope helps
<?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"]; } if(isset($_get["images"])) { $image = $_get["images"]; //$result = $xml->xpath('/root/area[photographer_id="' . $photographer_id . '"] , /root/area[photographer="' . $photographer . '"]');
}
//echo $photographer_id; //echo $photographer; //echo $image; $filteredresult = array(); foreach($result $obj){ if(in_array($photographer, (array)$obj) == 1 || $photographer == 'x'){ if(in_array($image, (array)$obj) || $image == 'x'){ array_push($filteredresult, $obj); } } } foreach($filteredresult &$obj){ //how access values in object echo $obj->{'photographer'}; echo $obj->{'image'}; } echo $obj->{'image'}; ?>
the last echo should give pointers
Comments
Post a Comment