Hi all,
I’m hoping someone can can point me in the right direction. I’ve been banging my head against this wall for a couple days now
I have a migx form entry set up that stores calendar events into a custom events table. This part is working perfectly - love it.
In this form, I have a multi-select “Event categories” field that let’s events be set to have multiple categories - to allow for filtering display on different ages and such.
These values are stored double-pipe delimited in the database like “category-1||category-2” which is fine.
In my calendar setup options, I want to be able to have a “categories” property that can be user filled to contain one or multiple categories of events they want to display, and probably comma delimited for simplicity. So entering “category-1,category-3” would allow you to display any events that contain either of those selected categories.
So how do I do this in my xpdo query - which is working aces, save for this
snippet below:
$thisStart = (isset ($_REQUEST['start'])) ? $_REQUEST['start'] : '';
$thisEnd = (isset ($_REQUEST['end'])) ? $_REQUEST['end'] : '';
$thisOrganization = (isset ($_REQUEST['organization'])) ? $_REQUEST['organization'] : '';
$thisCategory = (isset ($_REQUEST['category'])) ? $_REQUEST['category'] : '';
if (!$modx->addPackage('siteEvents',MODX_CORE_PATH.'components/siteEvents/model/','modx_')) {
die('Can\'t load package, try again later.');
}
$class = 'SiteEvent';
#$output = "[";
$c = $modx->newQuery($class);
$c->cacheFlag === false;
$c->select('id, title, organization, organizationImg, startDate as start, url as eventURL, endDate as end, category, colour as color, description');
$c->where(
array(
'published' => '1',
'startDate:>='=>$thisStart,
'startDate:<'=>$thisEnd
));
if($thisOrganization){
$thisOrganizationArray = explode(',', $thisOrganization);
$c->where(array('AND:organization:IN'=>$thisOrganizationArray));
}
if($thisCategory){
$thisCategoryArray = explode(',', $thisCategory);
$x=0;
foreach($thisCategoryArray as $item) {
$x++;
if($x<2){
$c->where(array('AND:category:IN'=>$item));
} else {
$c->where(array('OR:category:IN'=>$item));
}
}
}
$c->sortby('startDate', 'ASC');
/* Get all rows */
$objects = $modx->getCollection($class, $c);
if (empty($objects)) {
$output .= '';
}
$allEvents = [];
/* Iterate through rows */
foreach ($objects as $object) {
array_push($allEvents, $object->toArray('',false,true));
}
$output = json_encode($allEvents);
return $output;
To help narrow it down, it’s this part that has me struggling - and it could be 180 degrees in the wrong direction… it’s probably my 10001 attempt at writing it
if($thisCategory){
$thisCategoryArray = explode(',', $thisCategory);
$x=0;
foreach($thisCategoryArray as $item) {
$x++;
if($x<2){
$c->where(array('AND:category:IN'=>$item));
} else {
$c->where(array('OR:category:IN'=>$item));
}
}
}