Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Multiple articles under one date


Go to End


7 Posts   2036 Views

Avatar
potion_maker

Community Member, 36 Posts

2 October 2008 at 7:47am

Edited: 02/10/2008 8:41am

I'm trying to get multiple articles to show up under one date. Right now my DataObject is retrieving all the right info, but I'm not sure how to manipulate the data once I have it. I'm printing something like using the code found in tutorial 2:

10-01-08
Article Name - Read More
10-01-08
Article Name - Read More
09-28-08
Article Name - Read More
09-28-08
Article Name - Read More

and so on. I only need to see the date once for each day however.

10-01-08
Article Name - Read More
Article Name - Read More
09-28-08
Article Name - Read More
Article Name - Read More

Avatar
Blackdog

Community Member, 156 Posts

2 October 2008 at 5:50pm

perhaps trying sorting the dataobject by date and then getting it to return a variable based on peaking back to the object which was returned previous in the data array and testing that against the current items date field.

then i the in the template build your page based on a if statement which can test for the variable.

If no date was present it is obviously the the start of the list
If the date is the same as the current date is belongs under the same list
If the date is different then it is a new date, start a new list.

Avatar
potion_maker

Community Member, 36 Posts

10 October 2008 at 6:10am

Edited: 11/10/2008 4:48am

Thanks Blackdog.
I've got my DataObject ($articles) full of all of the data I need. I'm not sure how to iterate through it with the if statements. Do I need to do something like
foreach($articles as $article) {
$curDate = $article->Date;
if($curDate != $prevDate) {
$prevDate = $curDate;
} else {
$prevDate = $prevDate}
}
return $prevDate;

How can I reference this variable from the .ss page? Do I need to declare it in .php first? I guess I'm also not clear on how to run an if statement on the .ss page. I appreciate any and all help is this is the last problem I'm facing before launching my new site.

Thanks

Avatar
Blackdog

Community Member, 156 Posts

14 October 2008 at 1:26am

<% if yourVariable = someValue %>
do this
<% else_if %>
else do this
<% end_if %>

Avatar
potion_maker

Community Member, 36 Posts

17 October 2008 at 8:28am

Blackdog,
I'm still having trouble referencing my variables from the .ss page. Here is what I have. Maybe you can see what I'm doing wrong.

Schedule.php:

<?php
class Schedule extends Page {
static $db = array();
static $has_one = array();
static $allowed_children = array('ArtistPage');
}
class Schedule_Controller extends Page_Controller {
function SortArtists($limit, $sort="Date ASC") {
$artists = DataObject::get("ArtistPage", "", $sort, "", $limit);
$prevDate = "1";
foreach($artists as $artist) {
$curDate = $artist->Date;
if($curDate != $prevDate) {
$prevDate = $curDate;
$showDate = 1;
} else if($curDate == $prevDate) {
$showDate = 0;
}
}
return($artists) ? $artists : false;
}
}
?>

and then Schedule.ss:

<div id="Content" class="typography">
<h2>$Title</h2>
$Content
<% control SortArtists(10) %>
<% if showDate == 1 %>
<h3>$Date</h3>
<h4><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></h4>
<br />
<% end_if %>
<% if showDate != 0 %>
<h4><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></h4>
<br />
<% end_if %>
<% end_control %>
</div>

Avatar
jhirm

Community Member, 21 Posts

22 October 2008 at 10:09am

Edited: 22/10/2008 10:09am

Hey dude, it's me.

I don't have time at the moment to look at the API in detail, but one simple way to do this would be to make a holder page type for the dates. You'll be creating an extra layer of depth in the content tree, but you'll be able to organize your pages easily by 'date' simply using the Children methods already defined in the controller. Very roughly, without code, something like:

Site
-> News (ArticleDateHolder)
->-> October 1, 2008 (ArticleHolder)
->->-> Article 1 (Article)
->->-> Article 2 (Article)
->->-> Article 3 (Article)
->-> October 2, 2008 (ArticleHolder)
->->-> Article 1 (Article)
->->-> Article 2 (Article)
->->-> Article 3 (Article)

If you prefer to just add articles without the extra layer, let me know and I'll take a closer look at the API so that I can figure out the best way to query for results. Off the top of my head, I think you'll want to nest two controller methods in the .ss file, one that pulls unique records based on date (one record for each unique date), then another that pulls records based on each date. Obviously these methods need to be defined in the .php file. The .ss template might look something like this (again, very roughly):

<% control GetUniqueDates %>
<span class="newsDateTitle">$Date.Format(m.d.Y)</span><br/>

<% control GetArticlesByDate(Date) %>
<div class="newsItem">
<h5><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></h5>
<span class="newsSummary">$Content.FirstParagraph <a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a></span>
</div>
<% end_control %>

<% end_control %>

Hope this helps. Again, I can try to take a closer look at the API in order to help you with the controller methods, if you decide to continue with the second (and perhaps more reasonable) option. Also, I just bought a guitar, and am going to start taking some lessons here.

Avatar
potion_maker

Community Member, 36 Posts

23 October 2008 at 6:22am

Yes, I have been thinking about just adding another level of depth by making the date's have their own page but it seems a little unnecessary. Also I'm not sure I could get the artists to show up under their respective parent date on the schedule page. Really I just need to know how to reference a variable i created on the .php page. It seems like it should be easy but I have had no luck so far.