Array/Section Merge
5 posts by 3 authors in: Forums > CMS Builder
Last Post: October 9, 2013 (RSS)
By clowden - October 3, 2013
A few months back I worked on a website for a client who needed to merge two different sections into one listing, events and training. I followed a tutorial here on the forums http://www.interactivetools.com/forum/forum-posts.php?postNum=2198678 and got the merge to work properly, or so I thought. The two sections are merged into one list, but they are not sorting properly. Events stay grouped with events and trainings with training. You can see an example at http://catalystconnection.org/trainings/ and on the homepage as well. The first two records (as of today) are both events, Oct 16 and Oct 25, however, the next one is a training and has a start date of Oct 3, as you can see the dates are out of order. You cannot tell by the page, but in the CMS each event/training has a start date which is how the items are being sorted.
Any help would be greatly appreciated as I am at a loss. I have uploaded the page which will probably explain things a little better than what I did. The code in question starts on line 90.
Thank you,
Chris
By ross - October 9, 2013
Hi Clowden
Thanks for posting!
My first thought here is that you are actually using a "date" field to set the date of your events. What I think is happening on the page though is your events are being sorted by the createdDate field instead of the "date" field you created. The reason this becomes a problem is because your events will be sorted based on the date you did the merge and not the date set with your actual "date" field.
If this is the case, your solution is going to be changing the sort order of this section to be based on your "date" field. This can either be done in the section editor on the Sorting tab for that section, or in the code on your page in the getRecords part with a "orderBy" attribute.
Does this make sense? Let me know what you think. Thanks!
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com
Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/
By clowden - October 9, 2013 - edited: October 9, 2013
Ross, everything was being sorted by the correct start_date and not the createdDate. We looked into this some more and found out something odd. A few things you will see below. The orderBy fields need to be set to start_date ASC and the startDateCompare($b, $a) needed to be set as $b, $a instead of $a, $b like in the tutorial that was followed earlier.
When the startDateCompage is set as startDateCompare($a, $b) and not $b, $a, it would choose 3 dates and sort them. The dates made no sense as to why the where chosen. No idea why it is working now as it seems only the order of a few things were reverse, but it is.
There was also another query above this one on the same table, but it was using a different variable that was not being called on the page, once it was removed the order of the events came together.
<?php
//INSERT ALL YOUR getRecord() CALLS HERE, REPLACING MINE
list($upcoming_events, $upcoming_eventsMetaData) = getRecords(array(
'tableName' => 'events',
'limit' => '3',
'orderBy' => 'start_date ASC',
));
list($upcoming_courses, $upcoming_coursesMetaData) = getRecords(array(
'tableName' => 'course_occurrences',
'where' => "removeDate >= NOW() OR removeDate='0000-00-00 00:00:00'",
'limit' => '3',
'orderBy' => 'start_date ASC',
));
// put all the records in one list
$allRecords = array_merge(
$upcoming_courses,
$upcoming_events
);
// sort them by date
function startDateCompare($b, $a) { return -strcmp($a['start_date'], $b['start_date']); }
usort($allRecords, 'startDateCompare');
// discard everything after the 3rd record
$newestRecords = array_slice($allRecords, 0, 3);
?>