Organizing Lists

13 posts by 2 authors in: Forums > CMS Builder
Last Post: February 8, 2010   (RSS)

By jtedescojam - February 3, 2010

1st... job well done with v. 2.02... love the file uploading feature!! And I love the custom.css feature... along with everything else.

I am applying cms to a school district site which has a page displaying a meeting agenda. I receive these agendas weekly... and as I receive them, I upload them. There's about 2 or 3 per month... in the sidebar, I have them listed:

January 11th, 2010
January 25th, 2010
February 2nd, 2010

These are all added with the assistance of a list section in CMSB... I have the fields for month_day, year, title, content, etc... I would like to insert a break between months, automatically... so it would read:
JANUARY
January 11th, 2010
January 25th, 2010

FEBRUARY
February 2nd, 2010

And when I add one that is "MARCH" it'll display a MARCH heading, without me having to split the code into seperated filtered sections for each month. I'm guessing you might return this post with a set of "if/else" statements... but I'm curious as to what you think would do it. let me know if you can.
John Tedesco

Creative Director



JAM Graphics

Re: [jtedescojam] Organizing Lists

By Chris - February 3, 2010

Hi jtedescojam,

Can you please post the complete PHP source code for your page? That way I'll be able to see your section name and field names to write you up a solution.
All the best,
Chris

Re: [chris] Organizing Lists

By jtedescojam - February 3, 2010

Sure... very simply put....

<?php foreach ($agendasRecords as $record): ?>

<p><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?> <?php echo $record['month_day'] ?> <?php echo $record['year'] ?></a></p><?php endforeach ?>

here's the page that displays the results.
http://www.pequannock.org/district/board-of-education/agendas2010.php
John Tedesco

Creative Director



JAM Graphics

Re: [jtedescojam] Organizing Lists

By Chris - February 5, 2010

Hi jtedescojam,

So you're using two textfields (month_day and year) instead of a date field? In your above example, month_day contains "January 11th", "January 25th," and "February 2nd"?

If that's the case, you can extract the month out of your month_day field and output headers whenever the month name changes, like this:

<?php $currentMonth = ""; ?>
<?php foreach ($agendasRecords as $record): ?>

<?php $words = explode(" ", $record['month_day']); ?>
<?php $monthNameInCaps = strtoupper(@$words[0]); ?>
<?php ?>
<?php if ($monthNameInCaps != $currentMonth): ?>
<?php $currentMonth = $monthNameInCaps; ?>
<p><b><?php echo $currentMonth; ?></b></p>
<?php endif; ?>

<?php ?>
<p><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?> <?php echo $record['month_day'] ?> <?php echo $record['year'] ?></a></p>

<?php endforeach; ?>


I hope this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Organizing Lists

By jtedescojam - February 5, 2010

yeah that's more than perfect.. nicely done. I didn't use the 'Date field' because I couldn't figure out how to filter results from 2009 and 2010 on the page. I originally started that way, but I couldn't figure it out.

here's the result: http://www.pequannock.org/district/board-of-education/agendas20102.php

Would you be able to write in next to each line what they mean? I'd like to try to decipher it so I can use it in other contexts, but i don't understand $currenMonth, $words, explode, etc... I see there's some extra code in there because I included the 'day' in the month_day field.. so I'm not sure how I could use this in order contexts.

thanks again.
John Tedesco

Creative Director



JAM Graphics

Re: [jtedescojam] Organizing Lists

By Chris - February 5, 2010

Hi jtedescojam,

You can do this with a date field by supplying a "where" clause like: "YEAR(date) = 2010". Or, if you want the year supplied by the visitor: "YEAR(date) = '".mysql_escape($_REQUEST['year'])."'".

Here are some line numbers for reference:

[01] <?php $currentMonth = ""; ?>
[02] <?php foreach ($agendasRecords as $record): ?>
[03]
[04] <?php $words = explode(" ", $record['month_day']); ?>
[05] <?php $monthNameInCaps = strtoupper(@$words[0]); ?>
[06] <?php ?>
[07] <?php if ($monthNameInCaps != $currentMonth): ?>
[08] <?php $currentMonth = $monthNameInCaps; ?>
[09] <p><b><?php echo $currentMonth; ?></b></p>
[10] <?php endif; ?>
[11]
[12] <?php ?>
[13] <p><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?> <?php echo $record['month_day'] ?> <?php echo $record['year'] ?></a></p>
[14]
[15] <?php endforeach; ?>



[04] "explode" is PHP's "split", splitting on a space, we create a list ($words) of "January", "11th". Then we convert the first item in that list (the month) into all caps. [05]

[01] $currentMonth represents the last month which we wrote a header for. When the script starts, it's set to "". For every record, we check [07] if the record's month (in all caps) is the same as the last header we wrote. If it isn't, we output a header [09] and remember which header was output last [08].

This approach relies very much on your records being in order. ;)

I hope this helps! :)
All the best,
Chris

Re: [chris] Organizing Lists

By jtedescojam - February 5, 2010

Okay so you sold me on the "Date" field instead... so I've changed it to use the 'date' field now. Can you re-write the code accordingly? sorry.

this is what I have now.

<?php foreach ($agendasRecords as $record): ?>
<p><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?><br /><span class="agendadate"><?php echo date("D, M jS, Y g:i:s a", strtotime($record['date'])) ?></span></a></p><?php endforeach; ?>
John Tedesco

Creative Director



JAM Graphics

Re: [jtedescojam] Organizing Lists

By Chris - February 5, 2010

Hi jtedescojam,

Sure thing, all you need to do is replace these two lines:

<?php $words = explode(" ", $record['month_day']); ?>
<?php $monthNameInCaps = strtoupper(@$words[0]); ?>


with this:

<?php $monthNameInCaps = strtoupper(date('F', strtotime($record['date']))); ?>
All the best,
Chris

Re: [jtedescojam] Organizing Lists

By Chris - February 5, 2010

Hi jtedescojam,

Can you please post the top of that file? I'd need to know which field you're filtering on.
All the best,
Chris