mjurmann Posted December 26, 2007 Share Posted December 26, 2007 Hello, I'm trying to search a string for an exact match. The only problem is, when I use the following code, it is returning any entries that contain the search term, rather than only the exact matches. <?php if (stristr($rowCats, '3')) { echo "checked=\"checked\""; } else {}?> If someone searches for 31, any entry with "3", "1", or "31" are returned, instead of just returning the entry with "31". I'd greatly appreciate it if someone could give me a tip or point me in the right direction. I don't mind doing research, I just can't figure out what to use besides stristr, which obviously isn't working the way I'd like it to. Thanks. Quote Link to comment Share on other sites More sharing options...
effigy Posted December 26, 2007 Share Posted December 26, 2007 Why not $str == '31'? Quote Link to comment Share on other sites More sharing options...
mjurmann Posted December 26, 2007 Author Share Posted December 26, 2007 Perhaps I should show you a few more lines so it makes a little bit more sense... <?php $optionsSelected = $_GET['cats']; $rowCats = implode(",",$optionsSelected); ........ if (stristr($rowCats, '3')) { echo "checked=\"checked\""; } else {}?> The problem is that I can't use $str == '31' because there are multiple numbers contained within the variable. As you can see, the variable is determined by the array, $_GET['cats']. I implode the array and assign the contents to $rowCats. Then I search $rowCats for '3'. That works fine. The problem is when someone searches for 31. Anything containing "3" and "1" are selected, along with "31". Is there a way to search the string for an exact match of 31? Quote Link to comment Share on other sites More sharing options...
effigy Posted December 26, 2007 Share Posted December 26, 2007 Yes, there is a regex solution, but how about in_array instead of implode? <pre> <?php $cats = array( 1, 31, 311, 131, 333 ); if (in_array(3, $cats)) { print 'Found 3 the first time.'; } array_push($cats, 3); if (in_array(3, $cats)) { print 'Found 3 the second time.'; } ?> </pre> Found 3 the second time. Quote Link to comment Share on other sites More sharing options...
mjurmann Posted December 26, 2007 Author Share Posted December 26, 2007 Does anyone know how to apply the same solution to a MySQL query as well? I've creating an advanced Search Feature that allows my users to check multiple boxes to help narrow down their search. The contents of the search is stored in an array, ['cats']. When they submit, I want a MySQL query to look through a table and pick out only the rows that contain ALL of the searched parameters contained within the $_GET['cats'] array. It works to an extent, however, when someone checks the "7" value box, it includes "17" or "27" in the results as well. Is there a way to use something other than LIKE to find the exact number in the array, rather than something that contains at least that number? Here is my code: <?php $cats = $_GET['cats']; $order = $_GET['order']; foreach($cats as $categories) { $catsFieldHolder = " LIKE '%$categories%' AND wp_categories_one_field.categories $catsFieldHolder"; } echo $catsFieldHolder; [quote]$cats = $_GET['cats']; $order = $_GET['order']; $optionsSelected = $_GET['cats']; foreach($cats as $categories) { $catsFieldHolder = " LIKE '%$categories%' AND wp_categories_one_field.categories $catsFieldHolder"; } echo $catsFieldHolder; $searchResult = "SELECT * FROM wp_posts JOIN wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id JOIN wp_DLM_DOWNLOADS on wp_posts.ID = wp_DLM_DOWNLOADS.id JOIN wp_categories_one_field on wp_posts.ID = wp_categories_one_field.objectid WHERE wp_categories_one_field.categories $catsFieldHolder ORDER BY wp_DLM_DOWNLOADS.hits $order"; echo $catsFieldHolder echoes out: LIKE '%11%' AND wp_categories_one_field.categories LIKE '%7%' AND wp_categories_one_field.categories LIKE '%5%' AND wp_categories_one_field.categories Quote Link to comment Share on other sites More sharing options...
effigy Posted December 26, 2007 Share Posted December 26, 2007 I want a MySQL query to look through a table and pick out only the rows that contain ALL of the searched parameters contained within the $_GET['cats'] array. Are you working with one field that contains a comma separated list? Quote Link to comment Share on other sites More sharing options...
mjurmann Posted December 26, 2007 Author Share Posted December 26, 2007 Are you working with one field that contains a comma separated list? Yes I am. Quote Link to comment Share on other sites More sharing options...
effigy Posted December 26, 2007 Share Posted December 26, 2007 Perhaps FIND_IN_SET? Quote Link to comment Share on other sites More sharing options...
mjurmann Posted December 26, 2007 Author Share Posted December 26, 2007 That worked. Thank you for all of your help effigy. Its true what they say. You are a Super Genius! Quote Link to comment 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.