Perry Mason Posted January 2, 2010 Share Posted January 2, 2010 This is driving me nuts... So I have a database which contains some double barreled names and I've been trying to optimize the search. To make it worse, some of them have hyphens and some just a space. Some have both. And, of course, there are also entries that are not double barrelled. For example, I have an entry called Arias-Stella reaction. Ideally, I would like the user to arrive at it by typing any of the following inthe search: Arias-Stella reaction Arias Stella reaction Arias-Stella Arias Stella Arias Stella etc. Here's what I've done so far (assuming each entry consists of no more than three words separated by hyphens ans/or spaces): <?php $q=mysqli_real_escape_string ($dbcnx, $_GET['q']); $t = trim($q); $t1 = explode("-", $t); $t2 = explode(" ", $t1[0]); $t3 = explode(" ", $t1[1]); echo "$t2[0]<br>$t2[1]<br>$t3[0]<br>$t3[1]"; $result = mysqli_query ($dbcnx, "SELECT * FROM main WHERE name LIKE '%$t2[0]%' OR name LIKE '%$t2[1]' OR name LIKE '%$t3[0]' OR name LIKE '%$t3[1]' ORDER BY name"); ?> There's an obvious problem with this approach: unused variables are assigned NULL value. If even one of the four search parameters is unused, the search returns the entire content of the database which kinda defies the point. Is there a simpler way around it? Am I sweating too much over it? If there's no simpler solution I'll just explode the hyphens and use the first array element as a search parameter. Using AND instead of OR doesn't help either since the end number of array elements varies from search to search. Quote Link to comment https://forums.phpfreaks.com/topic/186896-exploding-double-barrelled-names-and-search/ Share on other sites More sharing options...
dreamwest Posted January 2, 2010 Share Posted January 2, 2010 mysql_query("SELECT * FROM main WHERE MATCH name AGAINST('{$q}') ") Quote Link to comment https://forums.phpfreaks.com/topic/186896-exploding-double-barrelled-names-and-search/#findComment-986988 Share on other sites More sharing options...
Perry Mason Posted January 2, 2010 Author Share Posted January 2, 2010 Thanks, it worked a treat. I guess I should've finished that Mysql for dummies before starting coding. Quote Link to comment https://forums.phpfreaks.com/topic/186896-exploding-double-barrelled-names-and-search/#findComment-986999 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.