H Posted May 28, 2007 Share Posted May 28, 2007 Hello, I am trying to make a website which has a search engine what can answer poeples questions, the only problem is they have to type the question exactly eg. 'what is vb' (shows the answer) 'what's vb' (does not show the answer). Is there any way I can make it search for the keywords so 'what' and 'vb' would show the answer without editing my code completely. HERES MY CODE! <p><font color="#999999"><span style="font-size: 15pt">Question Asked<br> </span></font><font size="2"> <?php $question = addslashes($_POST['question']); echo $question; ?> </font></p> <p><font color="#999999"><span style="font-size: 15pt">Answer<br> </span></font><font size="2"> <?php $host = 'localhost'; $user = '?'; $pass = '?'; $db = '?'; $connect = mysql_pconnect($host, $user, $pass); $selectdb = mysql_select_db($db); mysql_connect($host, $user, $pass); mysql_select_db($db); $SQLq = "SELECT * FROM `search` WHERE `question` LIKE '$question'"; $sql = mysql_query($SQLq)or die(mysql_error()); $fetch = mysql_fetch_array($sql); $answer = $fetch['answer']; if($answer == ""){ echo 'Sorry, I dont know the answer to that one.'; } else { echo $answer; } ?> </font></p> NOTE: THE USERNAMES ETC HAVE BEEN REMOVED FROM THE CODE Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/ Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 maybe use explode & implode $search = "what is vb"; $parts = implode(" ", $search); $search= explode(" or ", $parts); echo $search; your need to clean this up but its a start Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263246 Share on other sites More sharing options...
H Posted May 28, 2007 Author Share Posted May 28, 2007 I might try that, can the preg_match function be used with arrays Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263248 Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 if you look at my last post, a few tweak and you should beable to get it working.. basically you final SQL query should be something like this $SQLq = "SELECT * FROM `search` WHERE `question` LIKE '%what%' AND `question` LIKE '%is%' AND `question` LIKE '%vb%' "; so you need to alter the basic script i wrote from what OR is OR vb to `question` LIKE '%what%' AND `question` LIKE '%is%' AND `question` LIKE '%vb%' Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263250 Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 hint: start = "`question` LIKE '%" middle = "%' AND `question` LIKE '%" end = "%'" Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263252 Share on other sites More sharing options...
H Posted May 28, 2007 Author Share Posted May 28, 2007 But it wont find it because the field in the mysql database is called question which has the 'what is vb' question in and php can only find it if it is the full string not just words from the string Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263255 Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 No.. "SELECT * FROM `search` WHERE `question` = 'what' will only find record(s) which ONLY have 'what' in the question field if you do "SELECT * FROM `search` WHERE `question` LIKE '%what%' it will find all that have 'what' anywhere in the field question.. heres the code i would use <?php $search = "what is vb"; $SQLq = "SELECT * FROM `search` WHERE "; $parts= explode(" ", $search); $start = "`question` LIKE '%"; $middle = "%' AND `question` LIKE '%"; $end = "%'"; $search = $start.implode($middle, $parts).$end; echo $SQLq.$search; $SQLq = $SQLq.$search; ?> NOTE it needs cleaning up (ie search on 0/1 word) will fail Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263258 Share on other sites More sharing options...
H Posted May 28, 2007 Author Share Posted May 28, 2007 Actully sorry, thats wrong what I said before. Please can you give me a little help, this is my first time using these functions (implode, explode) Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263259 Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 see last post basically have 90% done Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263260 Share on other sites More sharing options...
H Posted May 28, 2007 Author Share Posted May 28, 2007 Can you put that in my code (above) I would appreciate it very much Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263262 Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 just a quick one (on the phone so kinda hard to do) <p><font color="#999999"><span style="font-size: 15pt">Question Asked<br> </span></font><font size="2"> <?php $question = addslashes($_POST['question']); echo $question; ?> </font></p> <p><font color="#999999"><span style="font-size: 15pt">Answer<br> </span></font><font size="2"> <?php $host = 'localhost'; $user = '?'; $pass = '?'; $db = '?'; $connect = mysql_pconnect($host, $user, $pass); $selectdb = mysql_select_db($db); mysql_connect($host, $user, $pass); mysql_select_db($db); $SQLq = "SELECT * FROM `search` WHERE "; $parts= explode(" ", $question); $start = "`question` LIKE '%"; $middle = "%' AND `question` LIKE '%"; $end = "%'"; $search = $start.implode($middle, $parts).$end; echo $SQLq.$search; $SQLq = $SQLq.$search; //$SQLq = "SELECT * FROM `search` WHERE `question` LIKE '$question'"; $sql = mysql_query($SQLq)or die(mysql_error()); $fetch = mysql_fetch_array($sql); $answer = $fetch['answer']; if($answer == ""){ echo 'Sorry, I dont know the answer to that one.'; } else { echo $answer; } ?> </font></p> Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263263 Share on other sites More sharing options...
H Posted May 28, 2007 Author Share Posted May 28, 2007 still wont show the correct answer if whats vb is typed in, try it www.ictdoc.com/temp Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263270 Share on other sites More sharing options...
H Posted May 28, 2007 Author Share Posted May 28, 2007 Fixed it, thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263277 Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 Well done also said its 90% done but NOTE it needs cleaning up (ie search on 0/1 word) will fail Quote Link to comment https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/#findComment-263282 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.