1

您好,我正在尝试使用组合框搜索 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() 提供的参数无效

搜索结果.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>             
4

1 回答 1

1

您看到的第一个问题是由于。

Holidays = $holidaysXML->xpath($searchedResult);

应该:

Holidays = $holidaysXML->xpath($resultsHoliday);

$resultsHoliday保存您的 xpath 查询的变量也是如此。

于 2012-05-01T14:15:10.610 回答