RCB Posted February 23, 2009 Share Posted February 23, 2009 hey guys i need some help making a ratio function i have a query that will return downloaded and uploaded values in raw form im using a byte converting function to convert the values into either b's, mb's ,gb's etc etc i want to make a function that will compare the upload vs download and make a ratio IE. 1.23 or 0.45 and also i want to sort the order by the ratio value im not sure how to do that either if anyone can help i would really appreciate it! thanks for the help!! here is my current code function byte_convert($bytes) { $symbol = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'); $exp = 0; $converted_value = 0; if( $bytes > 0 ) { $exp = floor( log($bytes)/log(1024) ); $converted_value = ( $bytes/pow(1024,floor($exp)) ); } return sprintf( '%.2f '.$symbol[$exp], $converted_value ); } $ipbwi->DB->allow_sub_select=1; $result = $ipbwi->DB->query(" SELECT u.downloaded,u.uploaded,u.uid FROM users u LIMIT 3 "); while ($r = $ipbwi->DB->fetch_row($result)) { $downloaded = ''.byte_convert(&$r[downloaded]).''; $uploaded = ''.byte_convert(&$r[uploaded]).''; $ratio = ''; } Quote Link to comment https://forums.phpfreaks.com/topic/146557-need-help-with-ratio-function/ Share on other sites More sharing options...
premiso Posted February 23, 2009 Share Posted February 23, 2009 Take the bytes uploaded and divide it by the bytes downloaded. This should give you the ratio you want. SELECT u.downloaded,u.uploaded, (u.uploaded/u.downloaded) as ratio ,u.uid FROM users u ORDER BY ratio LIMIT 3 If that does not work for the order by, change it to ORDER BY (u.uploaded/u.downloaded) Quote Link to comment https://forums.phpfreaks.com/topic/146557-need-help-with-ratio-function/#findComment-769401 Share on other sites More sharing options...
RCB Posted February 23, 2009 Author Share Posted February 23, 2009 thx m8 worked great! one more question though, how do i limit the output to 2 decimal point? x.xx instead of x.xxxx thx again for the help! Quote Link to comment https://forums.phpfreaks.com/topic/146557-need-help-with-ratio-function/#findComment-769417 Share on other sites More sharing options...
premiso Posted February 23, 2009 Share Posted February 23, 2009 MySQL round SELECT u.downloaded,u.uploaded, round((u.uploaded/u.downloaded), 2) as ratio ,u.uid Quote Link to comment https://forums.phpfreaks.com/topic/146557-need-help-with-ratio-function/#findComment-769420 Share on other sites More sharing options...
RCB Posted February 23, 2009 Author Share Posted February 23, 2009 thx a lot for your help! Quote Link to comment https://forums.phpfreaks.com/topic/146557-need-help-with-ratio-function/#findComment-769577 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.