Jump to content

need help with ratio function


RCB

Recommended Posts

 

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 = '';


   }

 

Link to comment
https://forums.phpfreaks.com/topic/146557-need-help-with-ratio-function/
Share on other sites

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) 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.