RobertFullmen Posted April 9, 2011 Share Posted April 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233213-keyword-search/ Share on other sites More sharing options...
Jnerocorp Posted April 9, 2011 Share Posted April 9, 2011 where are u searching for information? in a mysql database? Quote Link to comment https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199337 Share on other sites More sharing options...
perky416 Posted April 9, 2011 Share Posted April 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199355 Share on other sites More sharing options...
RobertFullmen Posted April 9, 2011 Author Share Posted April 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199368 Share on other sites More sharing options...
ted_chou12 Posted April 9, 2011 Share Posted April 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199479 Share on other sites More sharing options...
akeane Posted April 10, 2011 Share Posted April 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199497 Share on other sites More sharing options...
RobertFullmen Posted April 10, 2011 Author Share Posted April 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199499 Share on other sites More sharing options...
akeane Posted April 10, 2011 Share Posted April 10, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199502 Share on other sites More sharing options...
RobertFullmen Posted April 10, 2011 Author Share Posted April 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199540 Share on other sites More sharing options...
akeane Posted April 10, 2011 Share Posted April 10, 2011 So the only one that needs to be 'translated' is the item number. This should be okay than? Quote Link to comment https://forums.phpfreaks.com/topic/233213-keyword-search/#findComment-1199689 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.