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

Attachments:

index_051.php 10K

By clowden - October 8, 2013

Anyone have any thoughts on this? Thanks.

By mizrahi - October 8, 2013

I have a similar issue and am interested in knowing this too.

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);

?>