...

View Full Version : PHP / Smarty - While Loop Help



Covet
11-24-2010, 01:52 AM
Hello all, first time poster - long time listener...

Can anyone kindly explain what I'm doing wrong here? I'm trying to list all online staff members and the time since they were last active right beside their name.

My php:


$sql = mysql_query("SELECT * FROM users WHERE laston>(unix_timestamp()-15*60) AND user_level>1 ORDER BY userid ASC");
$a = array();
while ($row = mysql_fetch_assoc($sql)) {
$la = time() - $row['laston'];
$unit = "secs";
if ($la >= 60) {
$la = (int) ($la / 60);
$unit = "mins";
}
if ($la >= 60) {
$la = (int) ($la / 60);
$unit = "hours";
}
if ($la >= 24) {
$la = (int) ($la / 24);
$unit = "days";
}
$a[] = $row;
}

$smarty->assign('STAFFON', $a);
$smarty->assign('LASTON', $la);
$smarty->assign('UNIT', $unit);


and then the template side...



{section name=i loop=$STAFFON}
<li><a href='viewuser.php?u={$STAFFON[i].userid}'>{$STAFFON[i].username}</a> {$LASTON} {$UNIT}</li>
{/section}


The problem I'm having is that it's showing like this:
Joe 6 mins
David 6 mins
Bob 6 mins

I can't figure out how to get it to display the right time since last action, for each different user. As far as I can see, it's giving everyone the same time as the staff member with the highest ID...

Of course I'm also open to suggestions or better ways to write the code

Any help is greatly appreciated

Covet

firepages
11-24-2010, 10:28 AM
never used smarty but you are assigning the time only once (e.g. out of the loop) try...


while ($row = mysql_fetch_assoc($sql)) {
$la = time() - $row['laston'];
$unit = "secs";
if ($la >= 60) {
$la = (int) ($la / 60);
$unit = "mins";
}
if ($la >= 60) {
$la = (int) ($la / 60);
$unit = "hours";
}
if ($la >= 24) {
$la = (int) ($la / 24);
$unit = "days";
}
$row['laston'] = $la;
$row['unit'] = $unit;
$a[] = $row;

}
$smarty->assign('STAFFON', $a);

############################# in template...
<a href='viewuser.php?u={$STAFFON[i].userid}'>{$STAFFON[i].username}</a> {$STAFFON[i].laston} {$STAFFON[i].unit}</li>



or something like that!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum