jj20051 Posted February 24, 2009 Share Posted February 24, 2009 One of my clients asked me to build a simple search engine script for their site. The problem is now he wants to make it get better results. At the moment it searches just for those keywords entered. ( Example: If you type in free hosting, "Free web hosting" won't come up, only sites with "free hosting" joined together like that... Please forgive me for my somewhat messy code. Config.php connects to the database. Example1: http://apenex.net/search.php?search=free%20hosting Example2: http://apenex.net/search.php?search=free%20web%20hosting As you see bravenet should come up on both queries, but it doesn't. It only comes up on an exact match. So any suggestions you may have, i'm willing to try. MY CODE: <?php include ('config.php'); $search = $_GET['search']; if ($search == NULL){ echo '<center><strong>You Didn\'t Type Anything In!</center></strong>'; } else { $query = "SELECT * FROM sites WHERE keywords LIKE '%".$search."%' ORDER BY totalrating DESC"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $yourtext = $row['description']; $title = $row['title']; $url = $row['url']; if ( strlen( $yourtext ) > 45 ) { $yourtext = explode( ' ',$yourtext ); $yourtext = array_slice( $yourtext, 0, 45 ); $yourtext = implode( ' ', $yourtext) . ' ...'; } else { } if ( strlen( $title ) > 11 ) { $title = explode( ' ',$title ); $title = array_slice( $title, 0, 11 ); $title = implode( ' ', $title) . ' ...'; } else { } echo '<table border="0" class="sample">'; echo '<tr><td><strong><a href='.$url.'><font size="2">'; echo $title; echo '</font></a></strong></td></tr>'; echo '</table>'; echo '<table width="580" border="0" class="sample">'; echo '<tr><td><font size="2">'; echo $yourtext; echo '</font></td></tr><tr><td><font color="green" size="2">'; echo $url; echo '</font></td></tr></table></font><br>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/146730-creating-php-search-script/ Share on other sites More sharing options...
jj20051 Posted February 24, 2009 Author Share Posted February 24, 2009 Still looking for help PLZ Quote Link to comment https://forums.phpfreaks.com/topic/146730-creating-php-search-script/#findComment-770479 Share on other sites More sharing options...
Stephen68 Posted February 24, 2009 Share Posted February 24, 2009 hmm.. try this query maybe? $query = "SELECT * FROM sites WHERE keywords LIKE \"%$search%\" ORDER BY totalrating DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/146730-creating-php-search-script/#findComment-770490 Share on other sites More sharing options...
jj20051 Posted February 24, 2009 Author Share Posted February 24, 2009 Well that did nothing. Thanks for the try though... :'( Quote Link to comment https://forums.phpfreaks.com/topic/146730-creating-php-search-script/#findComment-770523 Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 The way you had it originally is correct. What you want to do is explode the search criteria at the space, then create a query based off of that. $search = $search . " "; // add a space so in case of 1 word this should work. $searchArr = explode(" ", $search); // split variables into separate words. $search = array(); foreach ($searchArr as $keyword) { if (!empty($keyword)) { $search[] = "`keywords` LIKE '%{$keyword}%'"; } } if (count($search) > 1) $search = "(" . implode(" AND ", $search) . ")"; else $search = $search[0]; // no need for the implode. $query = "SELECT * FROM sites WHERE {$search} ORDER BY totalrating DESC"; Give that a try and see what comes of it. Quote Link to comment https://forums.phpfreaks.com/topic/146730-creating-php-search-script/#findComment-770556 Share on other sites More sharing options...
jj20051 Posted February 24, 2009 Author Share Posted February 24, 2009 Praise You! Thanks!! That's what i've been trying to do all morning. Do you work freelance? If so I'd love to hire you... Quote Link to comment https://forums.phpfreaks.com/topic/146730-creating-php-search-script/#findComment-770580 Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 Praise You! Thanks!! That's what i've been trying to do all morning. Do you work freelance? If so I'd love to hire you... On occasion. Feel free to PM for contact info. Quote Link to comment https://forums.phpfreaks.com/topic/146730-creating-php-search-script/#findComment-770603 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.