prw Posted February 4, 2011 Share Posted February 4, 2011 Hello all! I'm having some issues with a snippet that I found online and have edited to how I want it. Basically the code works at the moment, except if I were to search for 2 or 3 terms, it would search the columns username/action/result/changes for any result that have any of the words rather than results that include both terms, instead of either. I want the query to be like (assuming 2 search terms): username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%' AND username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%' and not: username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%' OR username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%' It needs an AND instead of an OR, but I'm not sure how to go around doing it. Here's the code as it is at the moment: $filter = ($_POST['filter']); $query = "SELECT * FROM activity WHERE"; $searchresult = explode(" ", $filter); foreach ($searchresult as $key => $filter) { $query .= " username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%'"; if ($key != (sizeof($searchresult) - 1)) $query .= " AND "; } $query .= "ORDER BY id DESC"; Sorry if this is a little confusing to understand, hope someone understands what I need. Link to comment https://forums.phpfreaks.com/topic/226629-searching-multiple-columns-for-multiple-words/ Share on other sites More sharing options...
cyberRobot Posted February 4, 2011 Share Posted February 4, 2011 It sounds like you just need to surround the "or" parts with parenthesis. (username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%') AND (username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%') Link to comment https://forums.phpfreaks.com/topic/226629-searching-multiple-columns-for-multiple-words/#findComment-1169671 Share on other sites More sharing options...
prw Posted February 4, 2011 Author Share Posted February 4, 2011 Ah that worked. How such a small change can make a difference! Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/226629-searching-multiple-columns-for-multiple-words/#findComment-1169684 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.