splitting list results into two columns

6 posts by 3 authors in: Forums > CMS Builder
Last Post: December 13, 2011   (RSS)

By petejdg - December 9, 2011

I have seen a few different examples but didn't get anything to work right for me. I have a calendar of events looping through by month and then record, but I am wondering if I can look at the months inputted and split them into two columns. They might not have twelve months filled in. This is an example:http://www.jdgordonadvertising.com/cfs_volunteer/calendar/index.php.

Here is my code so far:
// load records
list($calendarRecords, $calendarMetaData) = getRecords(array(
'tableName' => 'calendar',
'orderBy' => 'date',
));

-----------------------------
<h1>Calendar Of Events</h1>
<?php
$half=intval($calendarMetaData['totalRecords']/2);
$count=0;
?>
<table width="300" border="0" cellspacing="0" cellpadding="3">
<tr><td>
<?php $heading = ""; ?>
<?php foreach ($calendarRecords as $event): ?>
<?php if($heading!=date("F, Y",strtotime($event['date']))): ?>
<?php $heading=date("F, Y",strtotime($event['date'])); ?>
<span class="highlightCaps"><?php echo $heading; ?><br /></span>
<?php endif ?>
<?php echo date("l, F jS",strtotime($event['date'])); ?><br />
<?php echo $event['location'] ?> | <?php echo $event['time'] ?><br /> <?php echo $event['title']; ?><br /><br />
<?php $count++; ?>
<?php endforeach ?></td>
</tr>
</table>

The code above has an attempt to look at the # of records and split it into two columns which I found in a post but that didn't work. I would like to be able to do it by how many months and then split.

Any help is appreciated. thanks.

Re: [petejdg] splitting list results into two columns

By ross - December 9, 2011

Hi there

Thanks for posting!

With a variable amount of data, having your data go horizontally first and then drop a row is usually the best way as it lets you control the output better.

If going vertically first is the only way this will work, do you think there will consistently be the same number of events per month? Or is that variable as well?

Let me know either way. 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/

Re: [ross] splitting list results into two columns

I would be fine with having the records go horizontally and then dropping a row. I had originally tried that but couldn't get that to work. I have no idea how many events there will be per month so that will be a variable.

Re: [petejdg] splitting list results into two columns

By Jason - December 12, 2011

Hi,

One approach would be to first sort your records into headings and then output half of your headings (and records) per column.

Try something like this:

<?php
// load records
list($calendarRecords, $calendarMetaData) = getRecords(array(
'tableName' => 'calendar',
'orderBy' => 'date',
));

//group arrays into headings
$headingsToRecords = array();

foreach ($calendarRecords as $record) {
$heading = date("F, Y", strtotime($record['date']));

if (!array_key_exists($heading, $headingsToRecords)) {
$headingsToRecords[$heading] = array();
}

$headingsToRecords[$heading][] = $record;
}

?>
-----------------------------
<h1>Calendar Of Events</h1>

<?php
$half = intval(count($headingsToRecords) / 2);
$count = 0;
?>

<table width="300" border="0" cellspacing="0" cellpadding="3">
<tr>
<td>
<?php $heading = ""; ?>
<?php foreach ($headingsToRecords as $heading => $events): ?>
<span class="highlightCaps"><?php echo $heading; ?><br /></span>
<?php if (++$count == $half): ?>
</td><td>
<?php endif ?>

<?php foreach ($events as $event): ?>
<?php echo date("l, F jS",strtotime($event['date'])); ?><br />
<?php echo $event['location'] ?> | <?php echo $event['time'] ?><br /> <?php echo $event['title']; ?><br /><br />
<?php endforeach ?>

<?php endforeach ?>
</td>
</tr>
</table>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [petejdg] splitting list results into two columns

By Jason - December 13, 2011

Hi,

I think I spotted the problem. Try this code:

<table width="300" border="0" cellspacing="0" cellpadding="3">
<tr>
<td>
<?php $heading = ""; ?>
<?php foreach ($headingsToRecords as $heading => $events): ?>
<span class="highlightCaps"><?php echo $heading; ?><br /></span>

<?php foreach ($events as $event): ?>
<?php echo date("l, F jS",strtotime($event['date'])); ?><br />
<?php echo $event['location'] ?> | <?php echo $event['time'] ?><br /> <?php echo $event['title']; ?><br /><br />
<?php endforeach ?>


<?php if (++$count == $half): ?>
</td><td>
<?php endif ?>

<?php endforeach ?>
</td>
</tr>
</table>


This performs the check to see if we've reached the half way point AFTER a heading has been outputted, not before. This should work better.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/