Michael Marcialis
Michael Marcialis

Reputation: 57

Assign values to PHP Smarty array via for loop

I'm a bit of a PHP newbie, but I'm trying to assign values (dates of the current week) to a smarty array through a for loop. Unfortunately, after an hour of searching, I'm unable to figure out how to pull this off.

The best I've been able to do is assign those date values to seven individual variables (as opposed to one array housing seven values). My code for that is below. Would anyone be able to assist me with having those values loaded into an array instead of individual variables? Thanks in advance for any help.

global $smarty;

// Set current date and parse about any English textual datetime description into a Unix timestamp
$ts = strtotime('now');

// Calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) $offset = 6;

// Calculate timestamp for the Monday
$ts = $ts - $offset*86400;

// This is where I want to assign dates to a smarty array, not individual variables
for ($i=0; $i<7; $i++, $ts+=86400){
    $smarty->assign('day'.$i,date("m/d/Y l", $ts));
}

Upvotes: 0

Views: 2253

Answers (1)

Sabeen Malik
Sabeen Malik

Reputation: 10880

Why dont you make the array like:

$days = array();
for ($i=0; $i<7; $i++, $ts+=86400){
    $days[] = date("m/d/Y l", $ts);
}
$smarty->assign('days' , $days);

Then in smarty you can use the foreach loop to display the array.

smarty would go something like:

{foreach from=$days key="ind" item="day"}
  {$day}<br />
{/foreach}

Upvotes: 2

Related Questions