forked from nathard/BookStore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlhint.php
60 lines (53 loc) · 1.42 KB
/
xmlhint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
// References SIT203 prac php-xml-livesearch
// create dom object
$xmlDoc=new DOMDocument();
$xmlDoc->load("books.xml");
$x=$xmlDoc->getElementsByTagName('book');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all book titles from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
{
$y=$x->item($i)->getElementsByTagName('title');
if ($y->item(0)->nodeType==1)
{
//find a link matching the search text
// string stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
//Returns all of haystack from the first occurrence of needle to the end.
// before_needle
//If TRUE, stristr() returns the part of the haystack before the first occurrence of the needle.
//needle and haystack are examined in a case-insensitive manner.
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
{
if ($hint=="")
{
$hint="<br /><a>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><br />";
}
else
{
$hint=$hint . "<a>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><br />";
}
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
$nohint="<br /><a><strong>No Suggestions</strong></a>";
if ($hint=="")
{
$response=$nohint;
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>