doufen9815
doufen9815
2012-05-01 13:55

使用标题和描述的组合框搜索XML

已采纳

hi im trying to search an XML document using a combo box so the user can search in either description or title. I have loaded the XML file to the page that i wish to display the searches on. The search box its self is attached to a top.php document. I have tried to store the variable and apply them to a XML search string. i get the following errors but am unsure how to proceed.

Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: Invalid expression in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11034582/public_html/web/searchResults.php on line 19

Warning: Invalid argument supplied for foreach() in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11034582/public_html/web/searchResults.php on line 22

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>             
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dtvjl64442 dtvjl64442 9年前

    The first problem you are seeing is caused because.

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

    Should be:

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

    as $resultsHoliday is the variable which holds your xpath query.

    点赞 评论 复制链接分享

相关推荐