Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Thanked 0 Times in 0 Posts

    PHP / Smarty - While Loop Help

    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>
    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


  • #2
    Super Moderator
    Join Date
    May 2002
    Perth Australia
    Thanked 101 Times in 99 Posts
    never used smarty but you are assigning the time only once (e.g. out of the loop) try...
    PHP Code:
    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;


    ############################# 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!
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts