Jump to content

finding keywords or phrases in one field


codenature

Recommended Posts

I am trying to make a form search through user input, and link it up with existing data  in the database.

 

so if in the text field, the person puts " flowers,  red roses, white roses, daisies, sunflowers, cars"

 

then they press "submit"

 

the code then runs sql to search the database with matching terms. So that if the terms in the database field row are

 

things->DBentry1->flowers, trucks, cars, red roses,

things->DBentry2->dogs, cats, flowers

 

it will echo out

 

" 2 entries have matches

 

"entry 1 has flowers, cars and red roses"

"entry 2 has flowers"

 

I want the results to be in order of number of matches returned for that database entry.

it will skip "trucks because trucks was not entered and so did not match.,

 

Basically I want the search form to let the user put the terms in separated by a comma and a space or without a space, and then php and mysql find the matching terms, then echo out the results in matching order from most to least.

 

I know that is a lot to ask of you all, but I would appreciate any help. Thanks in advance.

Link to comment
Share on other sites

Thank you. Also, I found this code on the net. Will this do it?

 

 

 

 

<?php
// checks if a search has been submitted
if(!empty($_REQUEST['search']))
{
  // the table to search
  $table = "yourTable";
  // explode search words into an array
  $arraySearch = explode(" ", $search);
  // table fields to search
  $arrayFields = array(0 => "title", 1 => "content");
  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $query = "SELECT * FROM ".$table." WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.")";
  $query_result = mysql_query($query);
  // print title
  echo '<h1>Your Search Results</h1>'."\n\n";
  if(mysql_num_rows($query_result) < 1)
  {
    echo '<p>No matches found for "'.$search.'"</p>';
  }
  else
  {
    echo '<p>Search Results for "'.$search.'":</p>'."\n\n";
    // output list of articles
    while($row = mysql_fetch_assoc($query_result))
    {
      // output whatever you want here for each search result
      echo '<a href="index.php?id='.$row['id'].'">'.$row['title'].'</a><br />';
    }
  }
}
else
{
  // display a welcome page
}
?>

 

 

Link to comment
Share on other sites

That does look like exactly what I described. Try it and see how it works for you.

 

Although that is searching more than one field and using AND.

 

If you want to match ALL words use AND. If you want to match any word, use OR.

Link to comment
Share on other sites

I have completed adjusting the code, and creating the database fields. "color" and "shape". the page reloads upon the "search " button submit, and echoes out nothing, and does nothing. Please help.

 

here is the php to process the query of the database.

 

<?php	
// checks if a search has been submitted
if(!empty($_POST['search']))
{

// Connects to your Database
				mysql_connect("xxxxxxxxxxxx", "xxxxxxxxxxxxx", "xxxxxxxxx") or die(mysql_error()); 
				mysql_select_db("ealiketwo") or die(mysql_error()) ;
  // the table to search
  $table = "users";
  // explode search words into an array
  $arraySearch = explode(" ", $search);
  // table fields to search
  $arrayFields = array(0 => "colors", 1 => "shapes");
  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $query = "SELECT * FROM ".$table." WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.")";
  $query_result = mysql_query($query);
  // print title
  echo '<h1>Your Search Results</h1>'."\n\n";
  if(mysql_num_rows($query_result) < 1)
  {
    echo '<p>No matches found for "'.$search.'"</p>';
  }
  else
  {
    echo '<p>Search Results for "'.$search.'":</p>'."\n\n";
    // output list of articles
    while($row = mysql_fetch_assoc($query_result))
    {
      // output whatever you want here for each search result
      echo '<a href="basicSearch.php?id='.$row['id'].'">'.$row['title'].'</a><br />';
    }
  }
}
else
{
  echo "oops";// display a welcome page
}




}
?>		

 

and here is the form for the search.

 

 <form id="search" name="search" method="post" action="">
             <input name="search" type="text" value="search" size="12" maxlength="200" />
    <input type="submit" name="search" id="search" value="search" />
  </form>

 

Link to comment
Share on other sites

I got it to work by removing the last "}", but it says upon the page reload after submit

 

Your Search Results

 

Search Results for "":

 

when I enter in a color like red.

 

I echoed out the array, it says this:

 

 

SELECT * FROM users WHERE (colors LIKE '%%') OR (shapes LIKE '%%')

 

looks wrong to me

Link to comment
Share on other sites

Rather than this business:

  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)

 

 

...

 

Use foreach();

 

foreach($arraySearch AS $searchKey=>$searchValue){
echo $searchValue;
//your code here to put in LIKE().
}

 

 

As for the problem,

$arraySearch = explode(" ", $search);

 

$search is never defined.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.