Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-26-2008, 03:24 PM   PM User | #1
runnerjp
Regular Coder

 
Join Date: Nov 2006
Posts: 601
Thanks: 1
Thanked 2 Times in 2 Posts
runnerjp can only hope to improve
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>';
}
?>
runnerjp is offline   Reply With Quote
Old 10-26-2008, 06:06 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 10-26-2008, 06:19 PM   PM User | #3
runnerjp
Regular Coder

 
Join Date: Nov 2006
Posts: 601
Thanks: 1
Thanked 2 Times in 2 Posts
runnerjp can only hope to improve
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();
runnerjp is offline   Reply With Quote
Old 10-26-2008, 07:44 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 10-26-2008, 07:58 PM   PM User | #5
runnerjp
Regular Coder

 
Join Date: Nov 2006
Posts: 601
Thanks: 1
Thanked 2 Times in 2 Posts
runnerjp can only hope to improve
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
runnerjp is offline   Reply With Quote
Old 10-26-2008, 08:07 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Sorry, wrap time() - 900 in brackets.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 10-26-2008, 08:16 PM   PM User | #7
runnerjp
Regular Coder

 
Join Date: Nov 2006
Posts: 601
Thanks: 1
Thanked 2 Times in 2 Posts
runnerjp can only hope to improve
ok working but i dont understand why " . time() - 900; is not in the sql statement?
runnerjp is offline   Reply With Quote
Old 10-26-2008, 08:21 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 10-27-2008, 02:07 AM   PM User | #9
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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..
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:08 PM.


Advertisement
Log in to turn off these ads.