使用 PHP 从 XML 文件中重新调整多个动态结果的 XPATH 方法 [英] 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">
<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
脚本捕获表单的结果并过滤用户输入的值,如下
Then the search_photo.php
script that catches the result of the form and filters the values entered by the user as follows
$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);
如果所有设置的都是 'photographer_id',则第一次 XPATH 传递的 $result
是正确的,如果我再尝试 $result = $xml->xpath('/root/area[photographer_id="' . $photographer_id . '"] |/root/area[photographer="' . $photographer . '"]');
并选择 1
和 fred
然后我得到一个所有四个数组的结果,当它应该是一个空数组时,可以建议如何纠正这个错误.抱歉,这里是 XML 文件
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 can advise how to correct this error.
Sorry michi here is the XML file
<?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 文件会比这个大.
does this help, the final XML file will be larger than this though.
推荐答案
有一个类似的问题,脚本很长,我从那里得到了这个想法并提出了这个我希望它有帮助
Hi had a simillar problem with a long in script, I have taken the idea from there and came up with this i hope it helps
<?php
$xml = simplexml_load_file("photo.xml");
$xml = simplexml_load_file("photo.xml");
for ($i = 0; $i
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"];
}
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'};
?>
最后的回声应该给你一些提示
the last echo should give you some pointers
这篇关于使用 PHP 从 XML 文件中重新调整多个动态结果的 XPATH 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!