我必须在网站上创建实时搜索。我必须使用PHP,但我从来没有研究过它,而且我几乎是编程的初学者。如何修复此PHP代码?
我有一个XML文件作为数据库,目的是获取节点值并将它们显示为用户键入内容时的建议。这段代码的问题是它将所有节点值吐出到网页上。
这里是PHP代码:
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("collection.xml");
$books = $xmlDoc->getElementsByTagName("book");
$q = $_GET["q"];
if (strlen($q) > 0) {
$hint = "";
foreach ($books as $book) {
$name = $book->getElementsByTagName("name")->item(0)->nodeValue;
echo "$name <br/>";
}
}
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
下面是XML文件:
<books>
<book>
<name>Harry Potter</name>
<quantity> 50 </quantity>
<price>19.90</price>
</book>
<book>
<name>Casino Royale</name>
<quantity> 50 </quantity>
<price>12.99</price>
</book>
<book>
<name>The Great Gatsby</name>
<quantity> 40 </quantity>
<price>14.90</price>
</book>
</books>
有人可以帮我解决这个问题,使我可以继续我的项目。提前感谢您的时间和帮助!非常感谢!
比较'$ name'到'$ q'?一个真正的数据库会更容易.. – chris85
添加您的XML文件的相关片段到您的文章。 – nvisser
请提及xml文件代码 –