Jump to content

XPATH method for retuning multiple dynamic results from a XML file using PHP


tony_p

Recommended Posts

XPATH method for retuning multiple dynamic results from a XML file using PHP
I 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 as

follows
<?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

 

Link to comment
Share on other sites

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>

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.