我有一个表格,用户输入或选择表格如下的值
<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 >" 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);
第一个XPATH传递中的$result
是正确的,如果我设置的所有内容都是'photographer_id',然后我尝试$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文件会比这更大。
答案 0 :(得分: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 />";
答案 1 :(得分:0)
不确定你想要什么,因为你的问题不太清楚。即便如此,我coded a little snippet向您展示了如何使用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
(
)
答案 2 :(得分:0)
你有一个长篇大论的simillar问题,我已经从那里采纳了这个想法并提出了这个我希望它有所帮助
<?php
$ xml = simplexml_load_file(“photo.xml”);
for($ i = 0; $ i&lt; 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 . '"] 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'};
?>
最后一个回应应该给你一些指示