mirkan03 Posted July 8, 2011 Share Posted July 8, 2011 Hello to all people on forum. I built web site with large database of some texts and for easier finding what you need on it, I wanted to include search engine! I built it, but it only works accurate with one word. Problem is when I want to search few word! If they are separated or in different order, search is failure. Here's my code: function trazi($table,$search) { $sve = mysql_query("SELECT id,headline,whole FROM $table ORDER BY id DESC"); $data = ""; while($tmp = mysql_fetch_array($sve)) { if(stristr($tmp['headline'], $search) || stristr($tmp['whole'], $search)) { $data .= '<a href="www.mysite.com/' . $table . '_cur.php?id=' . $tmp[0] . '" target="_blank" style=" text-decoration:none; color:#1A5C1F; font-size: 13px;">'; $data .= '<p>' . $tmp[1] . '</p>'; $data .= '</a>'; } } return $data; } So, I want search to be success only if ALL words can be found in headline OR in whole (text)!! Can you help me modify my code?! Thx anyway! Quote Link to comment https://forums.phpfreaks.com/topic/241438-help-in-search-engine/ Share on other sites More sharing options...
Eiolon Posted July 8, 2011 Share Posted July 8, 2011 You do not seem to have a WHERE clause. Example: SELECT id, headline, whole FROM $table WHERE headline = 'YOUR STRING HERE' OR whole = 'YOUR STRING HERE' Not sure if wanted the words to be exact or not, but that is for exact. Quote Link to comment https://forums.phpfreaks.com/topic/241438-help-in-search-engine/#findComment-1240292 Share on other sites More sharing options...
mirkan03 Posted July 8, 2011 Author Share Posted July 8, 2011 I wanted that searched words are included in headline or within whole (text)... Im not sure, but your code is not what I need, friend!!! It will work only if searched word and tables (headline or whole) are equal! I need to upgrade my function so I can find result if I seek for 2 or more words..Its working perfect as it is now, but only for 1 word!!! Quote Link to comment https://forums.phpfreaks.com/topic/241438-help-in-search-engine/#findComment-1240304 Share on other sites More sharing options...
Cineex Posted July 8, 2011 Share Posted July 8, 2011 I have not tested it but it should work. This is just what i got out of my head right not . it's very dirty and i would not suggest using it if you have a lot of users. REMEMBER to check the terms for evil activity be for passing it to a function like this!!!! (sql-injection). function search ($term) { $idArray = array(); $i = 0; $info; $searchTerms = explode(" ",$term); foreach($searchTerms as $word) { $currentIdArray = array(); $wordsSql = mysql_query("SELECT id FROM table WHERE headline LIKE '%$word%'"); while($headline = mysql_fetch_array($wordsSql)) { $currentIdArray[$headline['id']] = $headline['id']; } if($i == 0) { $idArray[$headline['id']] = $headline['id']; }else{ if(!in_array($headline['id'],$idArray)){ unset($idArray[$headline['id']]); } } $i++; } $j = 0; foreach($idArray as $id) { $sql = mysql_query("SELECT something FROM table WHERE id='$id'"); $info[$j] = mysql_fetch_assoc($sql); $j++; } return $info; } Quote Link to comment https://forums.phpfreaks.com/topic/241438-help-in-search-engine/#findComment-1240317 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.