AUTO SEARCH AND RSS FEED

Course- PHP TUTOTRIAL >

Auto Complete Search

The Auto complete search box provides the suggestions when you enter data into the field. Here we are using xml to call auto complete suggestions. The below example demonstrate, How to use auto complete text box using with php.

Index page

Index page should be as follows −

<html>
   <head>
      
      <style>
         div {
            width:240px;
            color:green;
         }
      </style>
      
      <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","livesearch.php?q="+str,true);
            xmlhttp.send();
         }
      </script>
      
   </head>
   <body>
      
      <form>
         <h2>Enter Course Name</h2>
         <input type="text" size="30" onkeyup="showResult(this.value)">
         <div id="livesearch"></div>
         <a href="http://www.aitechtonic.com">More Details </a>
      </form>
      
   </body>
</html>

livesearch.php

It is used to call the data from xml file and it will send the result to web browsers.

<?php
   $xmlDoc=new DOMDocument();
   $xmlDoc->load("autocomplete.xml");
   $x=$xmlDoc->getElementsByTagName('link');
   $q=$_GET["q"];
   
   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) {
            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>";
               }
            }
         }
      }
   }
	
   if ($hint=="") {
      $response="Please enter a valid name";
   }
   else 
   {
      $response=$hint;
   }
   echo $response;
?>

autocomplete.xml

It contained auto complete data and accessed by livesearch.php based on tittle field and Url filed

<pages>

   <link>
      <title>android</title>
      <url>http://www.aitechtonic.com/android/index.htm</url>
   </link>

   <link>
      <title>Java</title>
      <url>http://www.aitechtonic.com/java/index.htm</url>
   </link>

   <link>
      <title>CSS </title>
      <url>http://www.aitechtonic.com/css/index.htm</url>
   </link>

   <link>
      <title>angularjs</title>
      <url>http://www.aitechtonic.com/angularjs/index.htm </url>
   </link>

   <link>
      <title>hadoop</title>
      <url>http://www.aitechtonic.com/hadoop/index.htm </url>
   </link>

   <link>
      <title>swift</title>
      <url>http://www.aitechtonic.com/swift/index.htm </url>
   </link>

   <link>
      <title>ruby</title>
      <url>http://www.aitechtonic.com/ruby/index.htm </url>
   </link>

   <link>
      <title>nodejs</title>
      <url>http://www.aitechtonic.com/nodejs/index.htm </url>
   </link>

</pages>

RSS

Really Simple Syndication is used to publish often updated information from website like audio, video, images, etc. We can integrate RSS feeds to a website by using Ajax and php. This code demonstrates how to show RSS feeds in our site.

Index.html

Index page should be as follows −

<html>
   <head>
      
      <script>
         function showRSS(str) {
            if (str.length==0) { 
               document.getElementById("output").innerHTML="";
               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("output").innerHTML=xmlhttp.responseText;
               }
            }
            
            xmlhttp.open("GET","rss.php?q="+str,true);
            xmlhttp.send();
         }
      </script>
      
   </head>
   <body>
      <p>Please Select an option to get RSS:</p>
      
      <form>
         <select onchange="showRSS(this.value)">
            <option value="">Select an RSS-feed:</option>
            <option value="cnn">CNN</option>
            <option value="bbc">BBC News</option>
            <option value="pc">PC World</option>
         </select>
      </form>
      <br>
      
      <div id="output">RSS-feeds</div>
      
   </body>
</html>

rss.php

rss.php has contained syntax about how to get access to rss feeds and return rss feeds to web pages.

<?php
   $q=$_GET["q"];
   
   if($q=="cnn") {
      $xml=("http://rss.cnn.com/rss/cnn_topstories.rss");
   }
   
   elseif($q=="bbc") {
      $xml=("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/americas/rss.xml");
   }
   
   elseif($q="pcw"){
      $xml=("http://www.pcworld.com/index.rss");
   }
   $xmlDoc = new DOMDocument();
   $xmlDoc->load($xml);
   
   $channel=$xmlDoc->getElementsByTagName('channel')->item(0);
   
   $channel_title = $channel->getElementsByTagName('title')
   ->item(0)->childNodes->item(0)->nodeValue;
   
   $channel_link = $channel->getElementsByTagName('link')
   ->item(0)->childNodes->item(0)->nodeValue;
   
   $channel_desc = $channel->getElementsByTagName('description')
   ->item(0)->childNodes->item(0)->nodeValue;
   
   echo("<p><a href='" . $channel_link . "'>" . $channel_title . "</a>");
   echo("<br>");
   echo($channel_desc . "</p>");
   
   $x=$xmlDoc->getElementsByTagName('item');
   
   for ($i=0; $i<=2; $i++) {
      $item_title=$x->item($i)->getElementsByTagName('title')
      ->item(0)->childNodes->item(0)->nodeValue;
      
      $item_link=$x->item($i)->getElementsByTagName('link')
      ->item(0)->childNodes->item(0)->nodeValue;
      
      $item_desc=$x->item($i)->getElementsByTagName('description')
      ->item(0)->childNodes->item(0)->nodeValue;
      
      echo ("<p><a href='" . $item_link . "'>" . $item_title . "</a>");
      echo ("<br>");
      echo ($item_desc . "</p>");
   }
?>