Jump to content

[SOLVED] Search string for exact match


mjurmann

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.