Trying to position through an array based on date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tf22raptor
Forum Newbie
Posts: 4
Joined: Tue Jul 27, 2010 8:01 am

Trying to position through an array based on date

Post by tf22raptor »

Hi all,

I have an array of page content that displays content to a JCarousel slider. At the moment the first slide would be (code below) page_01.html, the 2nd slide page_02.html, third page_03.html etc.

Now what I am trying to do is setup this piece of code so that the content that is loaded into Jcarousel is determined by what date it is, so I need to be able to position like a starting point of the array based on date. Say its the 8th week of the year well I need to be able to make 'page_08.html' be the first content to load, page_09.html would be second, page10.html would be third etc and the preceeding pages (07, 06, 05 etc) would be left out of the content list.

I mean I coudl probably setup up 52 cases for an if statement but I have 52 content.html pages which would mean a LOT of code, just wondering if someone can help me figure out how to create something I am trying to do. Any help would be great. Here is my current array code:

Code: Select all

<?php

// Array indexes are 0-based, jCarousel positions are 1-based.
$first = max(0, intval($_GET['first']) - 1);
$last  = max($first + 1, intval($_GET['last']) - 1);

$length = $last - $first + 1;

// ---

$images = array(
	'page_01.html',
	'page_02.html',
	'page_03.html',
	'page_04.html',
	'page_05.html',
	'page_06.html',
	'page_07.html',
	'page_08.html',
	'page_09.html',
	'page_10.html',
	'page_11.html',
	'page_12.html',
	'page_13.html',
	'page_14.html',
	'page_15.html',
);

$total    = count($images);
$selected = array_slice($images, $first, $length);

// ---

header('Content-Type: text/xml');

echo '<data>';

// Return total number of images so the callback
// can set the size of the carousel.
echo '  <total>' . $total . '</total>';

foreach ($selected as $img) {
    echo '  <image>' . $img . '</image>';
}

echo '</data>';

?>
Last edited by Benjamin on Tue Jul 27, 2010 9:40 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: Trying to position through an array based on date

Post by Gargoyle »

use week numbers as array indexes, then use date('W') to get the current week number and use this as your slice offset value.
Post Reply