Here's what I'm trying to do. I have a list of 'tags' inside a field. I'm trying to allow users to drill down search results using those tags. Here's what the field looks like:
Say a user wants to search for 10 fr, male, intermittent catheter. I can't use %like% because some of the tags are similar (ex: male, female). Is there a way to break it into an array and search as you would in php? Or something similar?
Ideally, I would like to seperate each keyword and compare each tag against the list to make sure they are ALL in there.
The closest I've been able to come up with is something like:
Select d.field_id_115 as tags,tag.tag_name, title.title, title.entry_id, cat_title.cat_name FROM finn_tagger tag LEFT JOIN finn_tagger_links link
ON tag.tag_id = link.tag_id
LEFT JOIN finn_channel_titles title
ON link.entry_id = title.entry_id
LEFT JOIN finn_category_posts cat
ON title.entry_id = cat.entry_id
LEFT JOIN finn_categories cat_title
ON cat.cat_id = cat_title.cat_id
Left JOIN finn_channel_data d
ON cat.entry_id = d.entry_id
WHERE cat_title.cat_url_title = 'catheter-supplies'
AND tag.tag_name ='male'
AND d.field_id_115 LIKE '%silicone%';
Alternatively, Each tag can also have it's own field, but I'm not sure how to make it check to ensure that if there are one or more tags, both tags are included.
So I would need to make sure that the entry_id has a record with each tag choice. I tried to do that with the following:
SELECT entry_id
FROM (Select title.title, title.entry_id, cat_title.cat_name FROM finn_tagger tag LEFT JOIN finn_tagger_links link
ON tag.tag_id = link.tag_id
LEFT JOIN finn_channel_titles title
ON link.entry_id = title.entry_id
LEFT JOIN finn_category_posts cat
ON title.entry_id = cat.entry_id
LEFT JOIN finn_categories cat_title
ON cat.cat_id = cat_title.cat_id
Left JOIN finn_channel_data d
ON cat.entry_id = d.entry_id
WHERE tag.tag_name IN ('silicone')
AND cat_title.cat_url_title = 'catheter-supplies'
UNION ALL
Select title.title, title.entry_id, cat_title.cat_name FROM finn_tagger tag LEFT JOIN finn_tagger_links link
ON tag.tag_id = link.tag_id
LEFT JOIN finn_channel_titles title
ON link.entry_id = title.entry_id
LEFT JOIN finn_category_posts cat
ON title.entry_id = cat.entry_id
LEFT JOIN finn_categories cat_title
ON cat.cat_id = cat_title.cat_id
Left JOIN finn_channel_data d
ON cat.entry_id = d.entry_id
WHERE tag.tag_name IN ('male')
AND cat_title.cat_url_title = 'catheter-supplies') as e
group by entry_id HAVING COUNT(*) > 1
But that just doesn't seem like the best way and it's pretty slow.
First, you'll select the comma-separated database field you want to match against as follows:
SELECT REPLACE(comma_separated_field, ',', ' ') FROM some_table
Next, to break down the words, you'll need to use explode()
as follows:
$search_term = '10 fr, male, intermittent catheter';
$search_term = explode(',', $search_term);
Finally, you'll want to match similar words with a like statement, note the spaces between the %
symbols. This approach will successfully search for multiple exact matches like you want:
$like = '';
for($q=0; $q < count($search_term); $q++){
if($q+1 != count($search_term)){
$like .= "db_field LIKE '% " . $search_term[$q] . " %' AND ";
}else{
$like .= "db_field LIKE '% " . $search_term[$q] . " %'";
}
}