category listings / finding last record

11 posts by 2 authors in: Forums > CMS Builder
Last Post: April 9, 2009   (RSS)

By rez - April 2, 2009 - edited: April 2, 2009

I read a couple of posts on how to find the last record in a list view. Something about an array. I couldn't get that to work in this case of using the category list code found here. I am generating xml.

<?php
$old_group = '';

foreach ($eventsRecords as $record):
$group = $record['date']; ?>

<?php if ($group != $old_group):?>
<events date="<?php echo date("d-m-Y", strtotime($record['date'])) ?>" color="0x99CC00"><event>
<?php endif ?>
<event>
<title><?php echo $record['title'] ?></title>
<group><?php echo $record['group'] ?></group>
<where><?php echo $record['where'] ?></where>
<when><?php echo $record['when'] ?></when>
<image><?php foreach ($record['image'] as $upload): ?><?php if ($upload['isImage']): ?><?php echo $upload['urlPath'] ?><?php endif ?><?php endforeach; ?></image>
<description><![CDATA[<?php echo $record['content'] ?>]]></description>
</event>
</events> //the problem is here. i want to list out all events in this category (date) and only have this tag at the end of them (see results below).
<?php $old_group = $group; ?>
<?php endforeach ?>



<events date="11-03-2009" color="0xFF9900">
<event>
<title>Nam ultrices. Quisque nulla. Morbi augue</title>
<group>GROUP TITLE</group>
<where>location</where>
<when>16th of March 2009</when>
<image>images/event2.jpg</image>
<description><![CDATA[this is a description
]]></description>
</event>
<event>
<title>Maecenas ornare placerat massa</title>
<group>GROUP TITLE</group>
<where>place</where>
<when>16th of March 2009</when>
<image>images/event1.jpg</image>
<description><![CDATA[ another description
]]></description>
</event>
<event>
<title>uis aliquam dapibus o</title>
<group>FRIENDS BIRTH DAYS</group>
<where>wherever</where>
<when>16th of March 2009</when>
<image>images/event1.jpg</image>
<description><![CDATA[another description
]]></description>
</event>
</events> // see how this is only after the three events (closing my category- <events date= ) then the next category starts:


<events date="21-03-2009" color="0xFF9900">



The top PHP code works fine when there is one event on a particular date. But when multiple events are on the same date, the </events> tag should be at the end of them, befor moving on the next category (date). The examples I found here to find the last item in a list were to do with the last record in the whole list of $eventsRecords.

The second code above, the xml results is what I need in the end. Same as this:

<events= "date 1">

<event>


event 1 info

</event>

<event>


event 2 info

</event></events>

<events= "date 2">//etc</events>




I tried moving things around, the if statements and my poor attempt at an array example but couldnt get that darn closing </events> tag at the end of multiple <event></event> items only. If an array is the answer, could you be specific. I saw the recent april first post of someone trying to find the last item in a list with your array response and dont know how to set that up or understand arrays in PHP. But hopefully, I am over complicating resolving this.



thanks

Re: [rez] category listings / finding last record

By rez - April 3, 2009

I hope I have been clear and can get some help today. I just need to control where that one little </events> tag goes to finish. I don't know that u r available on the weekends.

Thanks

Re: [rez] category listings / finding last record

By ross - April 6, 2009

Hi there.

We aren't in on the weekends but here I am now :).

Based on the code you posted, the idea I started thinking about is already something you've setup for a different part of this page.

What you'll want to do is where the </event> tag needs to go, check if the current month equals the previous month. If it doesn't, add the </event> tag.

It's pretty much the same thing you are doing with the $old_group = ''; variable. Each loop, you are checking if the group name is the same.

What you'll want to do here is create another variable $lastDate and use it like this with the </events> tag:

<?php if $lastDate == date("d-m-Y", strtotime($record['date'])): ?>
</events>
<?php endif ?>

Does that make sense? My code isn't exactly setup for copy paste but I think that should give you an idea of what to do.

Let me know what you come up with :).
-----------------------------------------------------------
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] category listings / finding last record

By rez - April 6, 2009

hmmm... wont that put it after every event? i only need it after all the events for each date, once at the end before the next date...

so it would be a "not equal too" but it probably wont add it for the final date...



I'll experiment again but do you know what I mean? There may be 3 events: <event>event info </event> but notice above in my pseudo code that the </eventS> only goes once after them, before the next date.



I've confused myself but wanted to get that in before you are gone for the day. And I'm on my way to opening day, O's and yankees and will experiment when I get back. :) ... if I can... ;)

Re: [rez] category listings / finding last record

By ross - April 7, 2009

Hi Rez

My code will be checking if the current date equals the previous date. If it does, it's still the same day and you don't need a closing </events> tag. If it is a different date, you need that closing tag.

I do agree that it will likely not create the last </events> tag but you can just move that outside of the foreach loop.

It's pretty much working the same way as the code you have for checking the groups.
-----------------------------------------------------------
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: [rez] category listings / finding last record

By rez - April 7, 2009

still can't figure it out :(

i need, "if the current date is a new date, put a </events> at the end of the last <event> in that date only."

1 of my tries results:
<events date="21-10-2010" color="0x99CC00"><event>
<event>
<title> Event 4</title>
<group>another group</group>
<where>somewhere</where>
<when>12pm sharp</when>
<image>/cmsAdmin/uploads/image04_001.jpg</image>
<description><![CDATA[Curabitur tempus rutrum purus. Praesent massa quam, tincidunt eu, sollicitudin quis, rutrum nec, tortor. Nulla facilisi. Phasellus euismod metus et lacus. Fusce nisi. Curabitur eu orci vitae augue pulvinar eleifend. Etiam eleifend dictum mauris.]]></description>
</event>

</events> // this is fine, it's after all events (it only worked because there is one event

<events date="11-04-2011" color="0x99CC00"><event>
<event>
<title>Same Day Test Event</title>
<group>test</group>
<where>patio</where>
<when>whenever</when>
<image></image>
<description><![CDATA[test description of an event]]></description>
</event>

</events> //this is not fine, its between two events on the same date


<event>
<title>Another Same Day Test</title>
<group>Test Group</group>
<where>a place</where>
<when>sometime on this day</when>
<image></image>
<description><![CDATA[Test Description again]]></description>
</event>


//this is where the above <events/> should go instead, because it is after the two events for this date.

</xml>




<<?php ?>?xml version="1.0" encoding="utf-8"?>
<xml>
<?php
// list a category name , then all its entries using a variable named group
$old_group = ''; // init blank var.
$prevDate = '';
foreach ($eventsRecords as $record):
$group = $record['date']; // load sub-group value from record.
$curDate = $record['date'];?>
<?php if ($group != $old_group):?>
<events date="<?php echo date("d-m-Y", strtotime($record['date'])) ?>" color="0x99CC00"><event>
<?php endif ?>
<event>
<title><?php echo $record['title'] ?></title>
<group><?php echo $record['group'] ?></group>
<where><?php echo $record['where'] ?></where>
<when><?php echo $record['when'] ?></when>
<image><?php foreach ($record['image'] as $upload): ?><?php if ($upload['isImage']): ?><?php echo $upload['urlPath'] ?><?php endif ?><?php endforeach; ?></image>
<description><![CDATA[<?php echo $record['content'] ?>]]></description>
</event>

<?php $old_group = $group; // retain sub-group name before moving to new record. ?>
<?php if ($prevDate != $curDate):?>
</events>
<?php endif ?>
<?php $prevDate = $curDate; ?>
<?php endforeach ?>

</xml>


Re: [rez] category listings / finding last record

By rez - April 7, 2009 - edited: April 7, 2009

Sorry that I'm not getting it Ross. I keep getting the </events> tag after every event, or only once after all the dates, at the bottom. With everything I've tried, it's confusing to me why I would even need to use another variable like $prevDate because i'm already using the date in the group, so i guess I'm just bnot placing things or resetting the variable in the right place.



I can't figure out how to find where the last </event> is with multiple <event> entries on 1 date. If i put the if statement outside of my current conditionals, it only ends up at the bottom of the entire xml, after all dates. :( But it's true, even if there are 30 </event> tags (30 events on one date) the </events> tag only goes after the last </event> on that date. I dont think there is a way to detect the last </event> on a date but there is way to detect a new date of course. The </events> tag goes before a new date, which will place it correctly. (but not before the first date at the top)

But on the last date listed, as you say, there will not be a new date to put it before. I guess I can handle that with a "last item on the list check" or something, but i haven't even got that far. :(

Re: [rez] category listings / finding last record

By ross - April 8, 2009

Hi rez

It's definitely possible so we can keep at it if you like. I think we are just missing each other on the setup.

I am curious if there might be another way to do it though. Instead of using the PHP to get those dates figured out, why don't you just let this page display as it is now (with the </events> after each event. Then, maybe there is something you can tweak in the application that receives this xml file so that it handles the dates the way you need.

I don't actually know what you are using this feed with though so I don't know if that will be easier. Let me know what you think. We'll keep at it here if you like :).
-----------------------------------------------------------
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] category listings / finding last record

By rez - April 8, 2009

Hi Ross. That's a good idea but unfortunately, I dont have the source for this application. Seems so simple in my head to make the tag end up before each new date but since I'm a new to this, it's not happening. Anyway, I'm going to give it another try today if you have an idea or clarification I'm looking forward to your input. Thanks for your help so far.