Search using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mfaisalwarraich
    New Member
    • Oct 2007
    • 194

    Search using php

    Hi everybody, im trying the following code to search for a string in the database. it is working fine. the problem is when i enter string for example "hello" this will look for hello in the database and if i enter "hello world" then i will look for hell world instead of hello and world. i want it to look for both hello and world. how i can do this. i want user to be able to find with the string and if the all string or any word of the string is available in the database then the results should be passed. i will be thankful if any help will be given to me.

    thanking you.
    Code:
    if($_REQUEST["key"] != ""){
    $key = $_REQUEST["key"];
      $q = "SELECT * FROM contents WHERE title LIKE '%".$key."%' OR content LIKE '%".$key."%'";
      $r = mysql_query($q);
      echo "We have found these pages that contain the entered keyword(s).<br/><br/>";
      while($rw = mysql_fetch_assoc($r)){
    	  if($rw["name"]=="home") $link = "index.php";
    	  else $link = "page.php?pg=" . $rw["name"];
    	  echo "Page: <a  class='readmore' href='".$link."'>".$rw["title"]."</a><br/><br/>"; 
    	  echo "Content :- " . str_replace($key,"<b><u>$key</u></b>",substr(strip_html_tags($rw["content"]),0,300)) . ".... <br /><br />";
      }
     } else
     {
     echo("<center><font color=red>Sorry...! Your Search Revealed No Results. Please Enter Search Keyword.</font></center>");
     }
  • devsusen
    New Member
    • Feb 2007
    • 136

    #2
    Well, u need to use the fulltext search of MySql for this purpose.

    Here is the quick solution:

    Code:
    $q = "SELECT * FROM contents WHERE MATCH (title, content) AGAINST ('".$key."' IN BOOLEAN MODE)";

    Comment

    • mfaisalwarraich
      New Member
      • Oct 2007
      • 194

      #3
      thanx a lot for a quick reply. but i have one issue dat when the $key is used here:

      Code:
      echo "Content :- " . str_replace($key,"<b><u>$key</u></b>",substr(strip_html_tags($rw["content"]),0,300)) . ".... <br /><br />";
      it will only bold the entire search string not different words like if i search for "hello world" it will only hello world together not if both words are found seperately. what will be the solution?

      Comment

      • devsusen
        New Member
        • Feb 2007
        • 136

        #4
        Yeh, it will bold only when the entire search string is found. So I think if you like to highlight the search words(s), then you need to split the search string into a array and highlight for each of those words in the array. I think a custom function will do in this case.

        Comment

        • mfaisalwarraich
          New Member
          • Oct 2007
          • 194

          #5
          thanx alot. here is what i tried but it not worked for me:

          Code:
          $key = $_REQUEST["key"];
          
            //$q = "SELECT * FROM contents WHERE title LIKE '%".$key."%' OR content LIKE '%".$key."%'";
            $q = "SELECT * FROM contents WHERE MATCH (title, content) AGAINST ('".$key."' IN BOOLEAN MODE)";
            $r = mysql_query($q);
            echo "We have found these pages that contain the entered keyword(s).<br/><br/>";
            while($rw = mysql_fetch_assoc($r)){
          	  if($rw["name"]=="home") $link = "index.php";
          	  else $link = "page.php?pg=" . $rw["name"];
          	  echo "Page: <a  class='readmore' href='".$link."'>".$rw["title"]."</a><br/><br/>"; 
          	  echo "Content :- " . str_replace($key,"<b><u>$key</u></b>",substr(strip_html_tags($rw["content"]),0,300)) . ".... <br /><br />";
          	
            }
          $keyText = $_REQUEST["key"];
          
          $keyText = explode(" ", $keyText);
          
          foreach($keyText  as $text2){
          
          $text = explode(" ", substr(strip_html_tags($rw["content"]),0,300));
          
          foreach($text as $contentText){
          if($contentText == $text2)
          {
           echo "<b>".$text."</b>";
          }
          }
          }
          i have also tried preg_match but i dont know how to use it. please help me to do this. thanking u.

          Comment

          • dlite922
            Recognized Expert Top Contributor
            • Dec 2007
            • 1586

            #6
            If I understand right, you need to search to find if any of the keywords are found?

            you can do it all in one query!

            Here's some pseudo-code :

            Code:
            $myKeyString = trim("$_REQUEST['keywords']) // ie "Foo Bar" 
            
            $myKeyArr = explode(" ",$myKeyString); 
            
            // build the query criteria string. 
            if(count($myKeyArr)=1)
            {
                // if its a one keyword. 
                $myCleanKey = mysql_real_escape_string($myKeyString); 
                $queryString = "table.fieldName LIKE '% $myCleanKey %'"; 
            }
            elseif(count($myKeyArr)>1)
            {
                // first add the entire string as one string to search for, for more accurate search results by pushing the entire string infront of the array. 
                $myKeyArr = array_mearch((array)$myKeyString,$myKeyArr); 
                foreach ($myKeyArr AS &$key) 
                {
                    $key = mysql_real_escape_string($key) // Prevents SQL Injection
                    $key = "table.fieldName LIKE '% $key %'"; 
                
                }
                // Now myKeyArr looks like this array("table.fieldName LIKE '% Foo %'","table.fieldName LIKE '% Bar %'); 
                // Add the original string as one and Put "OR"s between the individual ones
                $queryString = "implode(" OR ",$myKeyArr); 
            }
            else
            {
                die("Error, Please Provide at least One keyword to search"); 
            }
            
            
            //then put this in your sql
            
            $sql = "SELECT blah blah blah FROM table WHERE ($queryString) AND moreBlah criteria ....."; // notice the parenthesis
            There's obviously more to this. You can add more validations.

            Let me know if you have any questions,






            Dan

            Comment

            • devsusen
              New Member
              • Feb 2007
              • 136

              #7
              I hope this should work for you -

              Code:
              $key = $_REQUEST["key"]; 
              
              $searchWords = explode(" ", $key); 
              $highlightWords = array();
              foreach($searchWords as $words) {
                 $highlightWords[] = "<b><u>".$words."</u></b>";
              }
                
                //$q = "SELECT * FROM contents WHERE title LIKE '%".$key."%' OR content LIKE '%".$key."%'"; 
                $q = "SELECT * FROM contents WHERE MATCH (title, content) AGAINST ('".$key."' IN BOOLEAN MODE)"; 
                $r = mysql_query($q); 
                echo "We have found these pages that contain the entered keyword(s).<br/><br/>"; 
                while($rw = mysql_fetch_assoc($r)){ 
                    if($rw["name"]=="home") $link = "index.php"; 
                    else $link = "page.php?pg=" . $rw["name"]; 
                    echo "Page: <a  class='readmore' href='".$link."'>".$rw["title"]."</a><br/><br/>";  
                    echo "Content :- " . str_replace($searchWords,$highlightWords,substr(strip_html_tags($rw["content"]),0,300)). "..... <br /><br />";
                    //echo "Content :- " . str_replace($key,"<b><u>$key</u></b>",substr(strip_html_tags($rw["content"]),0,300)) . ".... <br /><br />"; 
                
                } 
              /*$keyText = $_REQUEST["key"]; 
                
              $keyText = explode(" ", $keyText); 
                
              foreach($keyText  as $text2){ 
                
              $text = explode(" ", substr(strip_html_tags($rw["content"]),0,300)); 
                
              foreach($text as $contentText){ 
              if($contentText == $text2) 
              { 
               echo "<b>".$text."</b>"; 
              } 
              } 
              } 
              */

              Comment

              • mfaisalwarraich
                New Member
                • Oct 2007
                • 194

                #8
                thank you devsusen for your help it worked for me. thanx alot :) and thank you for pseudo code also. it is also a good information for me.

                thank u once again.

                faisal

                Comment

                Working...