我有一个用户输入或选择一个值的表单,表单如下
<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);
如果设置的$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 文件会比这更大。