尝试在搜索建议中获取非对象错误的属性

时间:2015-06-06 06:56:50

标签: php jquery xml

您好我正在尝试为我正在创建的移动应用创建搜索建议。在搜索开始自动搜索时开始提供建议。一切都工作正常,我也正在得到正确的输出,但问题是我收到通知试图获得非对象的属性,我不知道它实际是什么。所以有人可以帮我解决这个问题。

我的代码是

search.html

<html>
<head>
<script>
 function showResult(str) {
 if (str.length==0) { 
 document.getElementById("livesearch").innerHTML="";
 document.getElementById("livesearch").style.border="0px";
 return;
 }
 if (window.XMLHttpRequest) {
 xmlhttp=new XMLHttpRequest();
 } else { 
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 xmlhttp.onreadystatechange=function() {
 if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
  document.getElementById("livesearch").style.border="1px solid #A5ACB2";
 }
}
xmlhttp.open("GET","http://localhost/donotdel/searchsuggestion.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
 <input type="text" size="30" onkeyup="showResult(this.value)">
 <div id="livesearch"></div>
 </form>

 </body>
 </html>

的search.php

 <?php
 header ('Content-Type: application/xml');
 header("Access-Control-Allow-Origin: *");

 $xmlDoc=new DOMDocument();
 $xmlDoc->load("http://localhost/donotdel/d/categories.xml");

 $x=$xmlDoc->getElementsByTagName('product');

 $q=$_GET["q"];

 if (strlen($q)>0) {
 $hint="";
 for($i=0; $i<($x->length); $i++) {
 $y=$x->item($i)->getElementsByTagName('name');
 if ($y->item(0)->nodeType==1) {
   if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
    if ($hint=="") {
      $hint= $y->item(0)->childNodes->item(0)->nodeValue;
    } else {
      $hint=$hint . $y->item(0)->childNodes->item(0)->nodeValue;
    }
  }
 }
 }
 }
 if ($hint=="") {
 $response="no suggestion";
 } else {
  $response=$hint;
 }
 echo $response;
 ?>

1 个答案:

答案 0 :(得分:0)

PHP源代码中的标题是错误的。您当前输出文本,输出无效XML。

错误消息应包含行号。在这一行中,您尝试访问变量的对象属性,但该变量不包含对象。

您正在使用DOM方法。没错,但这是一种更简单的方法 - 使用XPath:

$xml = <<<'XML'
<products>
  <product>
    <name>Product One</name>
  </product>
  <product>
    <name>Product Two</name>
  </product>
</products>
XML;

$q = "product";

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

$hint = '';
foreach ($xpath->evaluate('//product') as $product) {
  $text = $xpath->evaluate('normalize-space(name)', $product);
  if (stristr($text, $q)) {
    $hint .= ' '.$text;
  }
}

var_dump(substr($hint, 1));

输出:

string(23) "Product One Product Two"

如果您想将XML输出到浏览器,请使用第二个文档来构建它。

header ('Content-Type: application/xml');
...
$target= new DOMDocument();
$suggestions = $target->appendChild($target->createElement('suggestions'));

foreach ($xpath->evaluate('//product') as $product) {
  $text = $xpath->evaluate('normalize-space(name)', $product);
  if (stristr($text, $q)) {
    $suggestion = $suggestions->appendChild($target->createElement('suggestion'));
    $suggestion->appendChild($target->createTextNode($text));
  }
}

$target->formatOutput = true;
echo $target->saveXml();

输出:

<?xml version="1.0"?>
<suggestions>
  <suggestion>Product One</suggestion>
  <suggestion>Product Two</suggestion>
</suggestions>