MyBB Hacks

Full Version: ANDing multiple values in a field
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

First of all, fantastic plugin, it's come in very useful. Now; I have a thread prefix/category setup where I have a relatively large number of possible values for one field (users may freely select any combination thereof). I'd like for it to be filtered such that selecting multiple values requires all of them to be present for threads to show up, like a logcial AND:

Quote:Topic 1
FIELD: A C D F

Topic 2
FIELD: A D F

Topic 3
FIELD: A C D

Selected filter: A C

==>

"Topic 1" and "Topic 3" are shown.

Currently, (and as per documentation) stacking filters works as an OR, meaning that it's not possible to filter for specific combinations within a single field (selecting A C D F would return all three Topics, which is not particularly helpful).

I was wondering whether there's a way to set it up so multiple filters for a multiple value field will be treated as filters across different keys/fields would be, or if not, whether there's a relatively unconvoluted way for me to hack it in on my copy of the plugin (I don't need OR filtering).

Anyone got any ideas?
Yes, except instead of being spread across three key/fields, it is all part of the same key/field, which is multiple select.

From the OP you linked:

(08-11-2012 08:00 PM)Jumper Wrote: [ -> ]prefix 1
values: value1, value2, value3, value4, value5
prefix 2
values: example1, example2, example3, example4, example5
prefix 3
values: test1, test2, test3, test4, test5

now i need a triple dropdown-menue, where you can select the values from prefix 1, prefix 2 and prefix 3, and for example: if you select the values "value2" & "example1" & "test4", and click on a search-button, results should only be displayed entries where this 3 values match.

possible?

What I'd like to do:

prefix 1
values: value1, value2, value3, value4, value5

If I select "value1" & "value3" & "value5" as a filter (all within prefix 1), results should only be displayed where an item has all three selected values.
Ah yea, that is why I split it all up into the categories - I did not want the overhead of a huge many options field. I think you need ZB or rateU - sorry Frown
Thanks for the help anyway! :)

I think I've figured it out in the meantime, although would still like to know if there is a less hacky solution.

For anyone looking to do something similar, this is the part that handles multiple select queries in xt_forumdhooks.php

Code:
if(!xthreads_empty($threadfield_cache[$field]['multival'])) {
				// ugly, but no other way to really do this...
				$qstr = '(';
				$qor = '';
				switch($filtermode) {
					case XTHREADS_FILTER_PREFIX:
						$cfield = xthreads_db_concat_sql(array("\"\n\"", $fieldname));
						$qlpre = "%\n";
						$qlpost = '';
						break;
					case XTHREADS_FILTER_ANYWHERE:
						$cfield = $fieldname;
						$qlpre = $qlpost = '';
						break;
					default:
						$cfield = xthreads_db_concat_sql(array("\"\n\"", $fieldname, "\"\n\""));
						$qlpre = "%\n";
						$qlpost = "\n%";
				}
				foreach($val2 as &$v) {
					$qstr .= $qor.$cfield.' LIKE "'.$qlpre.xthreads_forumdisplay_filter_parselike($v, $filtermode).$qlpost.'"';
					if(!$qor) $qor = ' OR ';
				}
				$qstr .= ')';
			}



Quote:$qstr .= $qor.$cfield.' LIKE "'.$qlpre.xthreads_forumdisplay_filter_parselike($v, $filtermode).$qlpost.'"';
if(!$qor) $qor = ' OR ';

This is where the multiple values are ORed together, you can just duct tape it with an AND:

Quote:$qstr .= $qor.$cfield.' LIKE "'.$qlpre.xthreads_forumdisplay_filter_parselike($v, $filtermode).$qlpost.'"';
if(!$qor) $qor = ' AND ';


Not a pretty solution by any means, but it does the job.
I just want to add an idea just by using templates, but we  need multiple checkbox fields with single value. Then modify the templates so it looks like one input.

For example, lets say we have 4 values, A, B, C and D

Option A:
Key: oa
Hide Thread Field: check all.
Input Field Type: Checkboxes
Value List: A
Multiple Value Delimiter: space or whatever
Filtering Mode: Exact match

Option B:
Key: ob
Hide Thread Field: check all.
Input Field Type: Checkboxes
Value List: B
Multiple Value Delimiter: space or whatever
Filtering Mode: Exact match

Option C:
Key: oc
Hide Thread Field: check all.
Input Field Type: Checkboxes
Value List: C
Multiple Value Delimiter: space or whatever
Filtering Mode: Exact match

Option D:
Key: od
Hide Thread Field: check all.
Input Field Type: Checkboxes
Value List: D
Custom Modify Error:
This one forces user to select at least one options.

Code:
<if !$mybb->input['xthreads_oa'] && !$mybb->input['xthreads_ob'] && !$mybb->input['xthreads_oc'] && !$mybb->input['xthreads_od'] then>
Please select at least one Options
</if>

Multiple Value Delimiter: space or whatever
Filtering Mode: Exact match

Insert this code inside the form (for example before {$posticons}) in template_prefix_newthread and template_prefix_editpost_first

HTML Code
<tr>
<td class="trow2" width="20%"><strong>Options</strong></td>
<td class="trow2">{$tfinput['oa']} {$tfinput['ob']} {$tfinput['oc']} {$tfinput['od']}</td>
</tr>


Then for filtering interface, insert this code inside the form in template_prefix_forumdisplay_searchforum_inline (don't forget to activate the Enable XThreads' Inline Forum Search setting:

HTML Code
<input type="checkbox" class="checkbox" name="filtertf_oa" value="A"{$GLOBALS['filters_set']['oa']['checked']['A']}  /> A
<input type="checkbox" class="checkbox" name="filtertf_ob" value="B"{$GLOBALS['filters_set']['ob']['checked']['B']}  /> B
<input type="checkbox" class="checkbox" name="filtertf_oc" value="C"{$GLOBALS['filters_set']['oc']['checked']['C']}  /> C
<input type="checkbox" class="checkbox" name="filtertf_od" value="D"{$GLOBALS['filters_set']['od']['checked']['D']}  /> D

Your solution is probably the best there is.  I'm not sure if there's a nice way to toggle OR/AND, so I picked the most likely to be used, OR.
Filtering on multi-valued fields is quite ugly, so I didn't really want to put much effort into it.  It's there for completeness sake, but the performance of the queries needed to achieve it is quite bad.
(09-24-2015 03:12 AM)ln_e Wrote: [ -> ]Thanks for the help anyway! Smile

I think I've figured it out in the meantime, although would still like to know if there is a less hacky solution.

For anyone looking to do something similar, this is the part that handles multiple select queries in xt_forumdhooks.php


Quote:$qstr .= $qor.$cfield.' LIKE "'.$qlpre.xthreads_forumdisplay_filter_parselike($v, $filtermode).$qlpost.'"';
if(!$qor) $qor = ' OR ';

This is where the multiple values are ORed together, you can just duct tape it with an AND:

Quote:$qstr .= $qor.$cfield.' LIKE "'.$qlpre.xthreads_forumdisplay_filter_parselike($v, $filtermode).$qlpost.'"';
if(!$qor) $qor = ' AND ';


Not a pretty solution by any means, but it does the job.

(09-29-2015 12:24 PM)ZiNgA BuRgA Wrote: [ -> ]Your solution is probably the best there is.  I'm not sure if there's a nice way to toggle OR/AND, so I picked the most likely to be used, OR.

i try this way, but not work. & filter mode i changed (exact macth, contains, wildcard)

for simple choosing (with little choice), i try RateU solution.
Reference URL's