Jump to content
MakeWebGames

How would i shorten this


Dave

Recommended Posts

I have made a new script yet at the top its quite chunky due to me using if's. Im sure there is a way to shorten it down.

if($_GET['time'] == "") { $q=mysql_query(sprintf("SELECT * FROM users WHERE laston>unix_timestamp()-15*60 ORDER BY laston DESC")); }
if($_GET['time'] == 1) { $q=mysql_query(sprintf("SELECT * FROM users WHERE laston>unix_timestamp()-1*60 ORDER BY laston DESC")); }
if($_GET['time'] == 5) { $q=mysql_query(sprintf("SELECT * FROM users WHERE laston>unix_timestamp()-5*60 ORDER BY laston DESC")); }
if($_GET['time'] == 10) { $q=mysql_query(sprintf("SELECT * FROM users WHERE laston>unix_timestamp()-10*60 ORDER BY laston DESC")); }
if($_GET['time'] == 15) { $q=mysql_query(sprintf("SELECT * FROM users WHERE laston>unix_timestamp()-15*60 ORDER BY laston DESC")); }
if($_GET['time'] == 30) { $q=mysql_query(sprintf("SELECT * FROM users WHERE laston>unix_timestamp()-30*60 ORDER BY laston DESC")); }
if($_GET['time'] == 60) { $q=mysql_query(sprintf("SELECT * FROM users WHERE laston>unix_timestamp()-60*60 ORDER BY laston DESC")); $_GET['time']=1; $unita="hours"; }
if($_GET['time'] == 720) { $q=mysql_query(sprintf("SELECT * FROM users WHERE laston>unix_timestamp()-720*60 ORDER BY laston DESC")); $_GET['time']=12; $unita="hours"; }
if($_GET['time'] == 1440) { $q=mysql_query(sprintf("SELECT * FROM users WHERE laston>unix_timestamp()-1440*60 ORDER BY laston DESC")); $_GET['time']=1; $unita="days"; }

Thanks in advanced.

Link to comment
Share on other sites

Re: How would i shorten this

lol => ereg('[^0-9]', $_GET['time'])

that's the ole "swatting a fly with a sledgehammer" deal.

why not use the native is_numeric() function?

 

By the way, wouldn't the order of that need to be switched up?

 

<?php
if(isset($_GET['time']) AND ereg('[^0-9]', $_GET['time'])) 
{ 
$time = $_GET['time'];
} else {
$time = 15;
}
?>

 

:O

Link to comment
Share on other sites

Guest Anonymous

Re: How would i shorten this

This has to be a classic example of the wrong question to ask...

Perhaps LostOne / Floydian can see what I mean here, surely a simple case of extract (the relevant) fields from the users over the maximal timespan is sufficient.

Link to comment
Share on other sites

  • 1 month later...

Re: How would i shorten this

Here is my version. Just to make an even shorter one.

<?PHP
$time = $_GET['time']+0;
$sum = time() - (60*($time==0?15:$time));
$q = mysql_query("SELECT * FROM `users` WHERE `laston` > $sum ORDER BY `laston` DESC");
?>

 

or you could place it directly inside the query and remove those back apostrophes (which are normally not useful):

 

<?PHP
$time = $_GET['time']+0;
$q = mysql_query("SELECT * FROM users WHERE laston > ".(time()-(60*($time==0?15:$time))=." ORDER BY laston DESC");
?>
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...