Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: time of viewing

  1. #1
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts

    time of viewing

    for some reaosn my code is displaying the page if its the last 1 they viewed or are viewing at any time at all.. how would i set it so its only showing those viewing the page in the last 15 minutes

    PHP Code:
    <?php $sql "SELECT * FROM useronline 
       WHERE 
     file LIKE 'http://www.runningprofiles.com/members/index.php?page=message%' or
      file LIKE 'http://www.runningprofiles.com/members/index.php?page=reply%' or
      file LIKE 'http://www.runningprofiles.com/members/index.php?page=mainforums%' or
     file LIKE 'http://www.runningprofiles.com/members/index.php?page=message%' or
      file LIKE 'http://www.runningprofiles.com/members/index.php?page=post%' AND
      timestamp < 900"
    ;
    $res mysql_query($sql) or die('Error: '.mysql_error());
    if(
    mysql_num_rows($res) > 0) {
      echo 
    '<div class="CurrentlyOnline">';
      while(
    $row mysql_fetch_assoc($res))
        echo 
    $row['user'].'<br />';
      echo 
    '</div>';
    }
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    900 seconds isn't enough, unless you're testing for something older than January 1, 1970 00:15.
    You want you're check to be time() - 900:
    PHP Code:
    timestamp " . time() - 900; 
    This will return any results that have occured more than 15 minutes ago. To reverse it, you'll want to do timestamp > " . time() - 900;. That will give you any results within the last 15 minutes.
    You can also do this through pure SQL.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    PHP Code:
     $sql "SELECT * FROM useronline 
       WHERE 
     file LIKE 'http://www.runningprofiles.com/members/index.php?page=message%' or
      file LIKE 'http://www.runningprofiles.com/members/index.php?page=reply%' or
      file LIKE 'http://www.runningprofiles.com/members/index.php?page=mainforums%' or
     file LIKE 'http://www.runningprofiles.com/members/index.php?page=message%' or
      file LIKE 'http://www.runningprofiles.com/members/index.php?page=post%' AND
      timestamp > ' . time() - 900 '"
    ;
    $res mysql_query($sql) or die('Error: '.mysql_error());
    if(
    mysql_num_rows($res) > 0) {
      echo 
    '<div class="CurrentlyOnline">';
      while(
    $row mysql_fetch_assoc($res))
        echo 
    $row['user'].'<br />';
      echo 
    '</div>';

    ok i have done it but for some reason it displays even the people who have been on the page for 2 days ect (these are people who closed the browser on the page)

    the time gets recorded by $timestamp = time();

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Change you're SQL precedence.
    Logically, the only place the time is being compared on is in conjunction with file LIKE 'http://www.runningprofiles.com/members/index.php?page=post%'.
    PHP Code:
    $sql "SELECT * FROM useronline 
       WHERE 
     (file LIKE 'http://www.runningprofiles.com/members/index.php?page=message%' or
      file LIKE 'http://www.runningprofiles.com/members/index.php?page=reply%' or
      file LIKE 'http://www.runningprofiles.com/members/index.php?page=mainforums%' or
     file LIKE 'http://www.runningprofiles.com/members/index.php?page=message%' or
      file LIKE 'http://www.runningprofiles.com/members/index.php?page=post%') AND
      timestamp > " 
    time() - 900
    Or you can move the timestamp up to the first check since the and has priority. Either way, you'll need the brackets unless you replace with an IN clause.
    Fixed you're quotations as well.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-900' at line 1

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sorry, wrap time() - 900 in brackets.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    ok working but i dont understand why " . time() - 900; is not in the sql statement?

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Because time() is a php function and not compatible with sql. You can do this with an sql command though, I believe its UNIX_TIMESTAMP() which returns an integer time.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fou-Lu View Post
    Because time() is a php function and not compatible with sql. You can do this with an sql command though, I believe its UNIX_TIMESTAMP() which returns an integer time.
    can use mysql function now(), in this case now() - 900 have same effect.

    Edit: checked and I was wrong, now() don't return unix timestamp

    best regards
    Last edited by oesxyl; 10-27-2008 at 02:11 AM.


  •  

    Posting Permissions

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