I'm using the W3C Live Ajax Search found here. Their code only searches one element, the "title". I would like to have it take the user's query and search through multiple elements, for example, the 'title' and the 'url'.
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("live.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
{
$d=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($d->item(0)->nodeType==1)
{
//find a link matching the search text
if (stristr($d->item(0)->childNodes->item(0)->nodeValue,$q) !=false)
{
if ($hint=="")
{
$hint=
$z->item(0)->childNodes->item(0)->nodeValue . "<br />" .
$d->item(0)->childNodes->item(0)->nodeValue;
}
else
{
$hint=$hint . "<br /><br />" .
$z->item(0)->childNodes->item(0)->nodeValue . "<br />" .
$d->item(0)->childNodes->item(0)->nodeValue;
}
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
Can somebody give me an example of how to make it work? From what I understand, stristr will only search one haystack and you can't use arrays, so is there another way? Thanks in advance!
Well, I figured out a way:
if ( (stristr($d->item(0)->childNodes->item(0)->nodeValue,$q) !=false) ||
(stristr($z->item(0)->childNodes->item(0)->nodeValue,$q) !=false) )
I doubt this is the optimal way, so if anybody has a better solution feel free to add it.
Related
https://www.w3schools.com/php/php_ajax_livesearch.asp
I am trying to modify the code in the tutorial for an ajax live search, to include a new field called keyword. I want the search to also search the keyword field in order to improve user experience.
I have already modified the link.xml fine to include the newly added keyword tag.
<link>
<title> title </title>
<keyword> keywords here </keyword>
<url>https://path to url</url>
</link>
I am kind of stuck in the area below, trying to modify the conditional statement.
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links 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');
$k=$x->item($i)->getElementsByTagName('keyword'); // New field added
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
I think you can change the condition
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
to
$checkTitle = stristr($y->item(0)->childNodes->item(0)->nodeValue,$q);
$checkKeyword = stristr($k->item(0)->childNodes->item(0)->nodeValue,$q);
if ($checkTitle || $checkKeyword) {
//
}
trying to add a Ajax search function to a xml file, and found this exemple :
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("my_xml_file.xml");
$x=$xmlDoc->getElementsByTagName('links');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links 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');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
This works perfect but the xml I have to search is a bit different and I don't know how to setup the function according to my needs.
This is how is formatted my xml :
<data name="my_name"><![CDATA[Data content]]></data>
I changed
`$x=$xmlDoc->getElementsByTagName('links'); to $x=$xmlDoc->getElementsByTagName('data');`
But how to change
$y=$x->item($i)->getElementsByTagName('title');
to have
$y=$x->item($i)-> name; and $z=$x->item($i)->Data content without CDATA ?;
Thanx a lot for your help !!!
add this in your loop:
$attr = $a->item($i)->getAttribute('name');
if($attr==""){
continue;
}
//take care of the $attr
echo $attr;
I am using AJAX live search to generate user-profile-specific links. It works well, I always end up at the profile I want to, but there ist an issue.
Let's do this for user 1 (username = testuser; user_id = 1; blogname = testblog). If I search for "test", both links will be displayed, the link to testuser's profile, and the link to testuser's blog. The strange thing now is, the links work as if they would look like this:
profile.php?user=1&page=profile
profile.php?user=1&page=blog
but the actual links look like this:
profile.php?user=%20+%201%20+%20&page=profile
profile.php?user=%20+%201%20+%20&page=blog
Since I end up on the page I want to, you could say it doesn't matter, but it does, because I need the $GET_['user'] values always to be real numbers, not that kind of stuff I'm dealing with, here.
I hope there is some easy way to fix this. Like nodeValue->string or something. I need to change the nodeValue in this part of the code I think: $z->item(0)->childNodes->item(0)->nodeValue
This is the code I'm using:
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("../xml/accounts.xml");
$x=$xmlDoc->getElementsByTagName('account');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links 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('username');
$b=$x->item($i)->getElementsByTagName('blogname');
$c=$x->item($i)->getElementsByTagName('companyname');
$z=$x->item($i)->getElementsByTagName('user_id');
//search for usernames
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
}
}
}
//search for blognames
if ($b->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($b->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no QuickResults, hit enter";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
Inside my XMLfile the structure looks like this, if it helps:
<account>
<username>testuser</username>
<user_id>1</user_id>
<blogname>testblog</blogname>
</account>
The problem you are getting arises from the fact that your code adds spaces and a plus sign to the resulting link. And spaces are automatically encoded as %20. The solution would be to remove them from the code like this:
$hint= "<a href='profile.php?user=" .
$z->item(0)->childNodes->item(0)->nodeValue .
"&page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
This change would need to be done in all four occurences.
I am using AJAX live search to generate user-profile-specific links. It works well, I always end up at the profile I want to, but there ist an issue.
Let's do this for user 1 (username = testuser; user_id = 1; blogname = testblog). If I search for "test", both links will be displayed, the link to testuser's profile, and the link to testuser's blog. The strange thing now is, the links work as if they would look like this:
profile.php?user=1&page=profile
profile.php?user=1&page=blog
but the actual links look like this:
profile.php?user=%20+%201%20+%20&page=profile
profile.php?user=%20+%201%20+%20&page=blog
Since I end up on the page I want to, you could say it doesn't matter, but it does, because I need the $GET_['user'] values always to be real numbers, not that kind of stuff I'm dealing with, here.
I hope there is some easy way to fix this. Like nodeValue->string or something. I need to change the nodeValue in this part of the code I think: $z->item(0)->childNodes->item(0)->nodeValue
This is the code I'm using:
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("../xml/accounts.xml");
$x=$xmlDoc->getElementsByTagName('account');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links 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('username');
$b=$x->item($i)->getElementsByTagName('blogname');
$c=$x->item($i)->getElementsByTagName('companyname');
$z=$x->item($i)->getElementsByTagName('user_id');
//search for usernames
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
}
}
}
//search for blognames
if ($b->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($b->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no QuickResults, hit enter";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
Inside my XMLfile the structure looks like this, if it helps:
<account>
<username>testuser</username>
<user_id>1</user_id>
<blogname>testblog</blogname>
</account>
The problem you are getting arises from the fact that your code adds spaces and a plus sign to the resulting link. And spaces are automatically encoded as %20. The solution would be to remove them from the code like this:
$hint= "<a href='profile.php?user=" .
$z->item(0)->childNodes->item(0)->nodeValue .
"&page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
This change would need to be done in all four occurences.
Trying to scrape a bit of basic account info from Pinterest pages (no I'm not scraping pins before I get accused of using this maliciously, it's simply a competitor research tool).
Some accounts work fine with file_get_html, others return completely blank objects and I can't figure out why. I've built the below test code with completely random pages of different sizes to try and do some testing... still no further forward.
It uses Simple HTML DOM and here is my test code trying to figure out why some aren't working.
$pinterestUrl1 = "https://uk.pinterest.com/sfashionality/";
$pinterestUrl2 = "https://uk.pinterest.com/serenebathrooms/";
$pinterestUrl3 = "https://uk.pinterest.com/jenstanbrook/";
$pinterestUrl4 = "https://uk.pinterest.com/homebaseuk/";
$pinterestUrl5 = "https://uk.pinterest.com/thedoifter/";
$pinterestUrl6 = "https://uk.pinterest.com/coolshitibuy/";
$html1 = file_get_html($pinterestUrl1);
$html2 = file_get_html($pinterestUrl2);
$html3 = file_get_html($pinterestUrl3);
$html4 = file_get_html($pinterestUrl4);
$html5 = file_get_html($pinterestUrl5);
$html6 = file_get_html($pinterestUrl6);
echo $pinterestUrl1 . " - "; if (is_object($html1)) { echo "Returns object okay<br/>"; } else { echo "Failed<br/>"; };
echo $pinterestUrl2 . " - "; if (is_object($html2)) { echo "Returns object okay<br/>"; } else { echo "Failed<br/>"; };
echo $pinterestUrl3 . " - "; if (is_object($html3)) { echo "Returns object okay<br/>"; } else { echo "Failed<br/>"; };
echo $pinterestUrl4 . " - "; if (is_object($html4)) { echo "Returns object okay<br/>"; } else { echo "Failed<br/>"; };
echo $pinterestUrl5 . " - "; if (is_object($html5)) { echo "Returns object okay<br/>"; } else { echo "Failed<br/>"; };
echo $pinterestUrl6 . " - "; if (is_object($html6)) { echo "Returns object okay<br/>"; } else { echo "Failed<br/>"; };
Result:
https://uk.pinterest.com/sfashionality/ - Returns object okay
https://uk.pinterest.com/serenebathrooms/ - Returns object okay
https://uk.pinterest.com/jenstanbrook - Failed
https://uk.pinterest.com/homebaseuk/ - Failed
https://uk.pinterest.com/thedoifter/ - Returns object okay
https://uk.pinterest.com/coolshitibuy/ - Returns object okay
I can't see any reasons why some of these return objects and others don't... and because it's blank I don't even know where to start debugging this kind of thing.
Any ideas at all on this one? Thanks
Simple HTML DOM parser has constant MAX_FILE_SIZE with value 600000 and URLs that you are requesting have slightly more HTML.
You can define MAX_FILE_SIZE with some larger value before including lib, this will produce a PHP notice but HTML will be processed. Code I have tested this with:
<?php
define('MAX_FILE_SIZE', 6000000); //Will produce notice, but we need to define it
include_once './simplehtmldom_1_5/simple_html_dom.php';
$urls = array(
'https://uk.pinterest.com/sfashionality/',
'https://uk.pinterest.com/serenebathrooms/',
'https://uk.pinterest.com/jenstanbrook/',
'https://uk.pinterest.com/homebaseuk/',
'https://uk.pinterest.com/thedoifter/',
'https://uk.pinterest.com/coolshitibuy/',
);
foreach ($urls as $url) {
$content = file_get_contents($url);
$html = str_get_html($content);
echo $url . ' - ';
if (is_object($html)) {
echo 'Returns object okay<br/>';
} else {
echo 'Failed<br/>';
};
}