Jump to content

Recommended Posts

Hi Everyone,

I'm trying to set up a system for my PHP MYSQL search that allows the user to type keywords. I've been doing this with one very long if statement, is there a better way? For example, I want to user to search for "chair" and have the thing return everything with a item number stating with 555. The length of the item number can vary between 10 and 12 numbers and that has slowed me down a bit.

 

Cheers, Rob

 

Link to comment
https://forums.phpfreaks.com/topic/233213-keyword-search/
Share on other sites

If it is a mysql database you are searching, use something like the followingy to display your results:

 

$query = mysql_query("SELECT * FROM table_name WHERE item_field LIKE '555%' AND keyword_field LIKE '%" . $_POST['search'] . "%'");

while ($row = mysql_fetch_assoc($query))

{

// echo results here

}

 

I haven't tested it but i use something along those lines in one of my scripts. If it doesnt work let me know and ill try and dig out the exact code.

Link to comment
https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199355
Share on other sites

where are u searching for information? in a mysql database?

Yes, thanks.

 

If it is a mysql database you are searching, use something like the followingy to display your results:

 

$query = mysql_query("SELECT * FROM table_name WHERE item_field LIKE '555%' AND keyword_field LIKE '%" . $_POST['search'] . "%'");

while ($row = mysql_fetch_assoc($query))

{

// echo results here

}

 

I haven't tested it but i use something along those lines in one of my scripts. If it doesnt work let me know and ill try and dig out the exact code.

Thank you for the response. This isn't exactly what I'm looking for because the there isn't anything like a keyword_field. I basically have, item number and description in the database so I need the to match up '555%' with the word 'Chair' (and other numbers for other words). I've been using an if statement saying that is 'search' = chair then search for 555%. But I have close to 200 such statements to write.

Link to comment
https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199368
Share on other sites

where are u searching for information? in a mysql database?

Yes, thanks.

 

If it is a mysql database you are searching, use something like the followingy to display your results:

 

$query = mysql_query("SELECT * FROM table_name WHERE item_field LIKE '555%' AND keyword_field LIKE '%" . $_POST['search'] . "%'");

while ($row = mysql_fetch_assoc($query))

{

// echo results here

}

 

I haven't tested it but i use something along those lines in one of my scripts. If it doesnt work let me know and ill try and dig out the exact code.

Thank you for the response. This isn't exactly what I'm looking for because the there isn't anything like a keyword_field. I basically have, item number and description in the database so I need the to match up '555%' with the word 'Chair' (and other numbers for other words). I've been using an if statement saying that is 'search' = chair then search for 555%. But I have close to 200 such statements to write.

Well, if you post some of your codes, then we can help you, what are the names of your columns? Example of your query? if you don't give enough information, that's the amount of output you'd get, you can surely do a id search and a keyword search with the same keywords:

SELECT * FROM table_name WHERE item_field LIKE '%{$_POST['search']}%' OR keyword_field LIKE '%{$_POST['search']}%'...

Ted

Link to comment
https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199479
Share on other sites

What about using and associative array with the  key being the word searching for?

 

Like:

 

$aKeywords = array('chair'=>555, 'table'=>123, 'bed'=>101, 'rug'=>350);

$inWord = 'chair';
if (array_key_exists($inWord, $aKeywords))
{
    echo $inWord."=".$aKeywords[$inWord];  // output:     chair=555
}
else
{
   echo $inWord." not found";
}

 

WHERE $inWord is the word you want to search for and the $aKeywords[$inWord] value is the translated value you want to look for in your database.

 

Link to comment
https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199497
Share on other sites

Here's what I have: 

$s_input = $_POST["search"]; 
$data = mysql_query("select * FROM inv_DB WHERE itm_num LIKE '$s_input' ||  Description LIKE '$s_input' ||    Price LIKE '$s_input'")
or die(mysql_error()); 

Print "<table border cellpadding=3>"; 
while($info = mysql_fetch_array( $data )) 
{ 
Print "<tr>"; 
Print 

"<th>" ;

echo '<img src="uploads/'.$info['img1'].'" width="153"  "height="271" />';

"</td> " ;
Print "<th>Item Number:</th> <td>".$info['itm_num'] . "</td> "; 
Print "<th>Description:</th> <td>".$info['Description'] . "</td> "; 
Print "<th>Price:</th> <td>$".$info['Price'] . "</td>  "; 


} 
Print "</table> "; 
?>

 

 

What about using and associative array with the  key being the word searching for?

 

Like:

 

$aKeywords = array('chair'=>555, 'table'=>123, 'bed'=>101, 'rug'=>350);

$inWord = 'chair';
if (array_key_exists($inWord, $aKeywords))
{
    echo $inWord."=".$aKeywords[$inWord];  // output:     chair=555
}
else
{
   echo $inWord." not found";
}

 

WHERE $inWord is the word you want to search for and the $aKeywords[$inWord] value is the translated value you want to look for in your database.

 

 

Thanks akeane, that does the trick aside from one thing. My search needs to check other fields besides the item number.

Link to comment
https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199499
Share on other sites

Are they all within the same db table?

If so then there would have to be a similar thing for each one?

Can you give an example?

 

Yes they're all in the same database. As demonstrated in the code I posted above, I have one search that looks at three fields in the database. One of field (item number) is the thing that needs the keyword.

Link to comment
https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199540
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.