doufen9815 2012-05-01 13:55
浏览 21
已采纳

使用标题和描述的组合框搜索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 2012-05-01 14:15
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器