Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder:
splitting list results into two columns

 

 


petejdg
User

Dec 9, 2011, 11:33 AM

Post #1 of 6 (698 views)
Shortcut
splitting list results into two columns Can't Post

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.


ross
Staff / Moderator


Dec 9, 2011, 4:44 PM

Post #2 of 6 (691 views)
Shortcut
Re: [petejdg] splitting list results into two columns [In reply to] Can't Post

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 - Product Specialist
support@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/



petejdg
User

Dec 11, 2011, 2:55 PM

Post #3 of 6 (674 views)
Shortcut
Re: [ross] splitting list results into two columns [In reply to] Can't Post

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.


Jason
Staff / Moderator


Dec 11, 2011, 11:23 PM

Post #4 of 6 (672 views)
Shortcut
Re: [petejdg] splitting list results into two columns [In reply to] Can't Post

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:


Code
<?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 - Programmer 
interactivetools.com

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


petejdg
User

Dec 12, 2011, 7:55 AM

Post #5 of 6 (668 views)
Shortcut
Re: [petejdg] splitting list results into two columns [In reply to] Can't Post

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.


Jason
Staff / Moderator


Dec 12, 2011, 9:48 PM

Post #6 of 6 (653 views)
Shortcut
Re: [petejdg] splitting list results into two columns [In reply to] Can't Post

Hi,

I think I spotted the problem. Try this code:


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 - Programmer 
interactivetools.com

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