Ning Developer Docs

Advanced Searching - building dynamic DB queries using BOOLEAN OR

Hi All I have my own source code and I'm implementing and advanced search form where a person can check off options what they are looking for ( HTML checkboxes ). The end result is that I want to do a query search using BOOLEAN OR...
Example:

Return results from all content where picture = "house" OR picture = "cat"
So based on the advanced search tutorial, I can get STATIC code working like this..

$query = $query->filter( XN_Filter::any(
XN_Filter('my->xg_profiles_answer_q30', 'eic', "cat",
XN_Filter('my->xg_profiles_answer_q30', 'eic', "house")
));

BUT the problem is I dont know how many options are going to be checked on the form for Q30..so I cant build a static query.

SO I tried building a string in PHP based on what is checked in the form like

$mystr="XN_Filter('my->xg_profiles_answer_q30', 'likeic', 'cat'),XN_Filter('my->xg_profiles_answer_q30', 'likeic', house')";

$query = $query->filter( XN_Filter::any( $mystr ));

and this does not work..because its not evaluating the subfilter. and this does not work either
$query = $query->filter( XN_Filter::any( 'house', 'cat' ));

Does anyone have any examples of BOOLEAN OR dynamic queries or have examples of how 'ANY' works.


Thanks
Ed

Views: 1

Replies to This Discussion

Hello Kram!

I was able to figure out a good way for you to do this! I found the answer by digging through the source code a bit:

// create a new query
$query = XN_Query::create("Content");
$query->filter('owner');

// generate a bunch of filters
$filters = array();
foreach ($ids as $id) {
$filters[] = XN_Filter('id', '=', $id);
}

// add the filters to the query
$query->filter(call_user_func_array(array('XN_Filter','any'), $filters));

// process the query
foreach ($query->execute() as $object) {
print $object->debuHtml();
}

Let me know if you have any questions!

Devin

RSS

© 2026   Created by Ning Developer Admin.   Powered by

Badges  |  Report an Issue  |  Terms of Service