splitting list results into two columns

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

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

I tried Jason's code below but it isn't working right:
http://siouxlandvolunteercenter.com/calendar/index.php

Ross - I would be okay with the horizontal layout as well, if you have an idea. thanks.

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/