我试图使用组合框搜索XML文档,以便用户可以搜索描述或标题。我已将XML文件加载到我希望显示搜索的页面。其自身的搜索框附加到top.php文档。我试图存储变量并将它们应用于XML搜索字符串。我得到以下错误,但不确定如何继续。
警告:SimpleXMLElement :: xpath()[simplexmlelement.xpath]:第19行/var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11034582/public_html/web/searchResults.php中的表达式无效
警告:第22行/var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11034582/public_html/web/searchResults.php为foreach()提供的参数无效
searchResults.php
<?php
include "top.php";
$holidaysXML = simplexml_load_file('holidays.xml');
if(isset($_POST["txtSearch"]) && $_POST["txtSearch"] != "Holiday Search..." && $_POST["txtSearch"] !="")
{
$txtSearch = $_POST["txtSearch"];
$searchOption = $_POST["searchOption"];
if($_POST["searchOption"] != "null")
{
$searchOption = $_POST["searchOption"];
$searchedResult = "Search results for \"$txtSearch\" in $searchOption";
$resultsHoliday = "channel/item [contains($searchOption, \"$txtSearch\")]";
}
}
$allHolidays = $holidaysXML->xpath($resultsHoliday);
foreach ($allHolidays as $currentHoliday)
{
echo "<a href= \"{$currentHoliday->link}\" style=\"font-weight:normal\" target=\"_blank\">{$currentHoliday->title}</a><br>";
echo "{$currentHoliday->pubDate}<br>";
echo "{$currentHoliday->description}";
}
This is where the information is gathered from the user on top.php
<form action = "searchResults.php" method = "post">
<input type = "text" name = "txtSearch" value = "Holiday Search...">
<select name = "searchOption">
<option value = "null" selected>(select)</option>
<option value = "title">Title</option>
<option value = "description">Description</option>
</select>
<input type = "submit" name = "search" value = "Search">
</form>
答案 0 :(得分:1)
您看到的第一个问题是因为。
Holidays = $holidaysXML->xpath($searchedResult);
应该是:
Holidays = $holidaysXML->xpath($resultsHoliday);
因为$resultsHoliday
是保存xpath查询的变量。