-1

I have a created a small query search for a xml file my problem is that when search with the photographer_id selected as it is a number ie 1,2,3 ect I get the rong result but if I change the form and xml file from 1 to one and 2 to two the search works fine I have tries casting the values from the get request and photographer_id to an integer with (int) and is numeric but same results

 $xml = simplexml_load_file("photo.xml");


   for ($i = 0; $i < count($xml); $i++){

    if(isset($_GET["LocationName"])) 

    {
       $photographer = $_GET["LocationName"];
    }

    $result = $xml->xpath('/root/area[photographer="' . $photographer . '"]  ');
    }


    if(isset($_GET["photographer_id"]))
    {
        $photographer_id = $_GET["photographer_id"];
    } 

    if(isset($_GET["images"]))
    {
        $image = $_GET["images"];
        }

    //echo $photographer_id;
    //echo $photographer;
    //echo $image;

    $filteredResult = array();

    foreach($result as $obj){
        if(in_array($photographer_id, (array)$obj)  || $photographer_id == 'x'){ 
            if(in_array($image, (array)$obj) || $image == 'x'){
                array_push($filteredResult, $obj);
            }
        }
    }

    foreach($filteredResult as &$obj){
        //how to access values in the object
        //echo $obj->{'photographer'};
        echo $obj->{'image'};
        }
        if (empty($filteredResult)) {
    echo 'Empty';
}

I have tried debug this piece of code but still can't figure out where I am going wrong

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

this question is not about XPATH, the XPATH search works fine its about why the search query 'one' returns correctly and yet if '1' is used the search pattern is incorrect

4

1 回答 1

2

这是我编辑的代码,包括所有内容(PHP 和 HTML,都在一页中)。出于安全原因,
我使用$_POSTover 。$_GET

<!-- here's the complete <head> etc. -->
<body>
<?php

$xml = simplexml_load_file('file.xml');

if (isset($_POST['submit'])) { // form has been sent, process and show results...

    // get data
    $id  = $_POST['id'];
    $img = $_POST['img'];

    if ($id <> '*') $id = "'$id'";
    if ($img <> '*') $img = "'$img'"; // wrap $id/$img in '' if is isn't *   

    $results = $xml->xpath("//area[photographer_id=$id and image=$img]");

    echo "<h1>search results</h1><p>results found: ". count($results) . "</p>";        

    // show results
    foreach ($results as $r)
    echo "result #" . ++$c . ": $r->photographer_id, $r->photographer, image: $r->image<br />"; 

} else { // form not sent, display form

    // get lists of unique ids / images from XML
    $ids = $imgs = array();
    foreach ($xml->area as $a) {
        $ids[(string)$a->photographer_id] = (string)$a->photographer;
        $imgs[(string)$a->image] =(string)$a->image;
    }
    ?>

<h1>Search</h1>
<form method="post">
    <label for="id">select a photographer</label>
    <select name ="id" id="id" size="1">
        <option value="*" selected="selected">all</option>
<?php // edit: typo corrected in NEXT LINE at value=\"$id\"
foreach ($ids as $id => $name) echo "<option value=\"$id\">$name ($id)</option>";
?>
    </select>
    <label for="img">select a photographer</label>
    <select name ="img" id="img" size="1">
        <option value="*" selected="selected">all</option>
<?php
foreach ($imgs as $img) echo "<option value=\"$img\">$img</option>";
?>
    </select>
    <input type="submit" name="submit" />    
</form>
<?php
} // if isset
?>
</body>
</html>

看到 PHP 部分工作:https ://eval.in/37100

于 2013-07-09T20:43:51.713 回答