Pages

Sunday 28 April 2013

Search suggestion using ajax,php and mysql

Create search engine with search suggestion like google using ajax,php and mysql.(search suggestion in php, how to show search suggestion using php and mysql,search. )

Step 1 : Create html text box (search.html )

<html>
         <title>search suggestion</title>
         <head>
        <style type="text/css">
               #search_item ul li{display:block;}
        </style>
        <script type="text/javascript">
               $(document).ready(function(){
               $('#search_item ul').click(function(event){
                      var data = $(event.target).text();
                      $('#search_box').val(data);
                      $("#search_item").hide();
                  });
            });
</script>
         </head>
        <body>
                  <form action="" method="post"> 
                  <input type="text" id="search_box" name="name" />
                   <input type="button" value="Search" id="search_button"/>
                  </form>
               <div class="search_item" id="search_item">
                    <ul>
                    </ul>
                </div>   
        </body>
</html>

Step 2 : Create database table

CREATE TABLE IF NOT EXISTS `table1` (
  `id` bigint(12) NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  PRIMARY KEY (`id`)
) ;


Step 3 : Create Ajax file ( ajax.js )


$(document).ready(function(){
 $("#search_box").keyup(function(){
         var search=$("#search_box").val();
         var dataString="item="+search;                      
        $.ajax({
                type:"POST",
                url:"/ajax.php",
                data:dataString,
                cache: false,
                    success:function(response){
                    $("#rr ul").html(response);
                    },
                    fail:function(error){
                        alert(error);
                        }
           });
     });   
});


Step 4 : Create php file ( ajax.php )

<?php 

        $searchq=$_POST["item"];
        $keyword =preg_replace('/\s+/', '', $searchq);
        $keyword =str_split($keyword);
        $query = "SELECT name  FROM table1" . "WHERE name LIKE '%".$keyword['0']."%'";
              for ($i=1; $i<count($keyword); $i++) {
                    $query = $query."AND  name LIKE '%".$keyword[$i]."%'";
              }
                  
                   $result = mysql_query($query);
                      while($row = mysql_fetch_assoc($result)) {
                              echo "<li>".$row['name']."</li>";
                         }  
 ?>



Monday 22 April 2013

Remove extra spaces from string in php

Removing the space into string using php.this problem facing many time by the php coder. i am trying to solve this problem here.remove extra space from string using php,remove space in php,how to hide extra space from string using php.

 Type 1 :  trim( ) Function

This function only remove space before and after the string
<?php
         $str="   This is only for test     ";
         echo trim($str);

?>
// output
        This is only for test


Type 2 :  preg_replace( ) Function

This Function remove space between the string

<?php 
 
     $str="This    is    only   for    test"; 
     $str = preg_replace("/\s+/"," ",$str);
        echo $str;
 
?>
//output
     This is only for test
 
 
if you like this please comment this 

Sunday 21 April 2013

About Technology and Life

Technology has adversely affected the quality of life and work because it is used inappropriately. For example, large companies working on large projects will often utilize the telephone to coordinate efforts of team members rather than bringing people together in the same location.