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.

Template Questions /

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

Accessing sub array in template


Go to End


2 Posts   1591 Views

Avatar
macka

Community Member, 33 Posts

26 August 2014 at 10:58am

Hi Guys,

I have an array i am passing to the template that looks like...
bookdates =
0 = 2014-08-26
1 = 2014-08-27
etc...

On my template i have..
<% loop $itemBookedOutDates %> $bookdates <% end_loop %>
but i can't seem to access the sub portions of the bookdates array? Normally i think it's would be something like print_r(bookdates[0]) etc to access that portion, but how do you do that in the template?

Thanks
Grant

Avatar
macka

Community Member, 33 Posts

26 August 2014 at 5:35pm

So what i have - is a function to get the dates between ranges and returns them as an array.

I then wanted to put those dates into a bit of javascript on my template page.

Just can't seem to find a way to get the code to put the values on the page. Any help would be great thanks!

What i have so far...

	public function getItemBookedOutDates() {
		
		$Params = $this->getURLParams();
		
		$itemDatesBookedOutOn = new item_booking();
		
                $itemDatesBookedOutOn = item_Booking::get()->filter(array(
				'Item' => $Params['ID']
		));				

		$DateList = new ArrayList();	
		
		foreach($itemDatesBookedOutOn as $item) {		

			$days = $this->createDateRangeArray($item->itemBookingFrom, $item->itemBookingTo);
	
			$item->DateUnavailable = $days;
			
			$DateList->push($item);
		}			

		return $this->DateList;
	}
	
	function createDateRangeArray($strDateFrom,$strDateTo)
	{
		// takes two dates formatted as YYYY-MM-DD and creates an
		// inclusive array of the dates between the from and to dates.

		// could test validity of dates here but I'm already doing
		// that in the main script		
		$aryRange=array();

		$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2), substr($strDateFrom,0,4));
		
		$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2), substr($strDateTo,0,4));
		
		if ($iDateTo >= $iDateFrom)
		{
			array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
			
			while ($iDateFrom < $iDateTo)
			{
				$iDateFrom += 86400; // add 24 hours
				
				array_push($aryRange,date('Y-m-d',$iDateFrom));			
			}
		}
		return $aryRange;
	}