xpath搜索php

时间:2012-04-13 12:27:44

标签: php xml xpath

我不断收到以下错误消息

Notice: Undefined index: txtSearch in     /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11023553/public_html/holiday/searchprocess.php on line 11

Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: Invalid expression in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11023553/public_html/holiday/searchprocess.php on line 22

Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: xmlXPathEval: evaluation failed in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11023553/public_html/holiday/searchprocess.php on line 22

Warning: Invalid argument supplied for foreach() in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11023553/public_html/holiday/searchprocess.php on line 26
Title

然后我尝试运行我的搜索文件

<?php
include('functions.php');

$header = makeHeader();


// create an instance
$holidayDoc =         simplexml_load_file('http://numyspace.co.uk/~unn_w11023553/holiday/holidays.xml');      

// set $studytype to either the upper case request or null
$txtSearch = $_GET["txtSearch"];

// set the query using the studytype
if (!is_null($txtSearch)) {
    $qry = "//channel/item[description[contains(text(),\"$txtSearch\")]]";
}
else {
// blank search entered so all holidays are shown.
    $qry = "/channel/'ALL'";
}

$holidays = $holidayDoc->xpath($qry);   // do the xpath query LINE 17
// now loop through all the students
echo "<table border=\"1\">\n";
echo "<tr><td>Title</td></tr>\n";
foreach ($holidays as $holiday) //LINE 21
{
echo 
echo <p><strong><a href = '$link'>'$holiday'</a></strong></p>
    <p><small>$published</small></p>";

}


$footer = makeFooter();

&GT;

已经摆弄了大约2个小时,仍然无法解决我出错的地方,任何见解都会非常感激,谢谢。

2 个答案:

答案 0 :(得分:0)

第一个警告告诉您txtSearch不是$_GET查询字符串参数数组中的值。例如。您在使用脚本调用脚本时没有?txtSearch=something

所以你需要检查你是如何调用脚本的。是否设置了querystring参数?你拼错了吗?

您可以通过执行$_GET来调试var_dump($_GET)的内容。

答案 1 :(得分:0)

替换

$txtSearch = $_GET["txtSearch"];

if( isset( $_GET["txtSearch"] ) && ( trim( $_GET["txtSearch"] ) != '' ) ) {
    $txtSearch = $_GET["txtSearch"];
} else {
    $txtSearch = null;
}

希望这有帮助。