Dave Posted July 24, 2008 Share Posted July 24, 2008 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. Quote Link to comment Share on other sites More sharing options...
Dave Posted July 24, 2008 Author Share Posted July 24, 2008 Re: How would i shorten this Thank you LostOne Quote Link to comment Share on other sites More sharing options...
Floydian Posted July 24, 2008 Share Posted July 24, 2008 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 Quote Link to comment Share on other sites More sharing options...
Floydian Posted July 24, 2008 Share Posted July 24, 2008 Re: How would i shorten this Na, you'll be alright :P Quote Link to comment Share on other sites More sharing options...
Guest Anonymous Posted July 25, 2008 Share Posted July 25, 2008 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. Quote Link to comment Share on other sites More sharing options...
a_bertrand Posted September 4, 2008 Share Posted September 4, 2008 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"); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.