-2

我有一个用户输入或选择一个值的表单,表单如下

<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 Now &gt;" id="Submit" class="Adv1_Filter_Button" type="submit">

然后search_photo.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);

如果设置的$result所有内容都是“photographer_id”,则第一个 XPATH 传递是正确的,如果我尝试$result = $xml->xpath('/root/area[photographer_id="' . $photographer_id . '"] | /root/area[photographer="' . $photographer . '"]'); 选择1然后fred我得到所有四个数组的结果,当它应该是一个空数组时可以建议如何纠正这个错误。抱歉,michi 这里是 XML 文件

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

这有帮助吗,最终的 XML 文件会比这更大。

4

3 回答 3

1

这是一种方法:
(1)从表单中获取过滤器值并为xpath构建一个查询字符串:

$fields = array( // fieldname => value for "all"
    'photographer_id' => '',
    'photographer' => 'x',
    'image' => '0');

foreach ($fields as $fieldname => $fieldvalue)
    if (isset($_GET[$fieldname]) && trim($_GET[$fieldname]) != $fieldvalue)
    $query[] = "$fieldname = '$_GET[$fieldname]'";

if (isset($query)) 
    $query = "[" . implode(' or ', $query) . "]"; else $query = "";

(2)使用xpath过滤XML并显示选中的节点

$xml = simplexml_load_string($x); // assume XML in $x
$results = $xml->xpath("area$query");

foreach ($results as $result) 
    echo "image $result->image by $result->photographer ($result->photographer_id)<br />";

看到它工作:http ://codepad.viper-7.com/ciboJg

于 2013-05-19T16:20:37.187 回答
0

不太确定你想要什么,因为你的问题不太清楚。尽管如此,我还是编写了一小段代码来向您展示如何使用 XPath 条件来检索area表单上给定选择所需的节点。

只需将$examples数组上的值视为表单上提交的可能值。

<?php
$xml = <<<XML
<?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>
XML;

$sxe = new SimpleXMLElement($xml);

$examples = array(
    array('id' => 1, 'name' => 'John'),
    array('name' => 'Fred'),
    array('id' => 1, 'name' => 'Fred'),
);

foreach ($examples as $example) {
    $conditions = array();

    if (!empty($example['id'])) {
        $conditions[] = "photographer_id = {$example['id']}";
    }

    if (!empty($example['name'])) {
        $conditions[] = "photographer = '{$example['name']}'";
    }

    $xpath_condition = implode(' and ', $conditions);
    echo "{$xpath_condition}\n";
    $area = $sxe->xpath("//area[{$xpath_condition}]");

    print_r($area);
}

输出:

photographer_id = 1 and photographer = 'John'
Array
(
    [0] => SimpleXMLElement Object
        (
            [photographer_id] => 1
            [photographer] => John
            [image] => a
        )

    [1] => SimpleXMLElement Object
        (
            [photographer_id] => 1
            [photographer] => John
            [image] => b
        )

    [2] => SimpleXMLElement Object
        (
            [photographer_id] => 1
            [photographer] => John
            [image] => c
        )
)
photographer = 'Fred'
Array
(
    [0] => SimpleXMLElement Object
        (
            [photographer_id] => 2
            [photographer] => Fred
            [image] => a
        )
)
photographer_id = 1 and photographer = 'Fred'
Array
(
)
于 2013-05-19T14:13:46.927 回答
0

嗨,脚本很长有一个类似的问题,我从那里得到了这个想法并想出了这个我希望它有所帮助

<?php

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

对于 ($i = 0; $i < 计数($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 . '"] and /root/area[photographer="' . $photographer . '"]');

}

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

$filteredResult = array();

foreach($result as $obj){
    if(in_array($photographer, (array)$obj) == 1 || $photographer == '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'};
}
echo $obj->{'image'};

    ?>

最后的回声应该给你一些指示

于 2013-05-20T14:40:01.590 回答