Page 1 of 1

[Solved] If and If

Posted: Tue Sep 08, 2009 12:25 am
by frozenarmageddon
Hey there, I am trying to make an Auto bytes converter, so it won't display the size as 45278489 [bytes] but as 45.278 MB.
The math part works flawless, but I got a problem with the IF statements...
I tried like 7 different ways to write it and none worked.
My most recent try:

Code: Select all

if($filesize <= "500000000") and if($filesize > "999999")
    {
        $filesizecalc = (round($filesize / 1000) / 1000);
        $filesizebyte = "MB";
    }
    else if($filesize <= "1000000") and if($filesize > "999")
    {
        $filesizecalc = ($filesize / 1000);
        $filesizebyte = "KB";
    }
I also tried stuff like

Code: Select all

if("999999" <= $filesize <= "500000000")
Most of my tries worked [No errors] but they didn't work as I wanted them to. (The way I wanted them to work is in the complete tagged code [not the last one, the one before.] which is pretty self explanatory), they just ignored the other else if's, and used the $filesizecalc and $filesizebyte setting of the first one...

I think I know a way to make it work, but that way is going to be really a pain to read and understand, and also pretty much double the size of the file, and make it messy...
I really appreciate any help, even if you say you don't know :D
Oh, and sorry for the bad English, I just woke up after a whole night working on this, and my brain is still kinda numb... :|

Re: If and If

Posted: Tue Sep 08, 2009 1:20 am
by cpetercarter

Code: Select all

 
    if ($filesize <= 500000000 && $filesize > 999999)
        {
            $filesizecalc = (round($filesize / 1000) / 1000);
           $filesizebyte = "MB";
        }
        elseif ($filesize <= 1000000 && $filesize > 999)
        {
            $filesizecalc = ($filesize / 1000);
            $filesizebyte = "KB";
       }
 

Re: If and If

Posted: Tue Sep 08, 2009 1:34 am
by requinix
Would you mind using 1024 instead of 1000? This whole kilo/kibi thing is kinda silly.

Re: If and If

Posted: Tue Sep 08, 2009 3:02 am
by Mark Baker

Code: Select all

 
function sizeIt($value) {
    $nameArray = array('bytes','KB','MB','GB','TB','PB');
 
    $i = 0;
    while ($value > 1024) {
        $i++
        $value /= 1024;
    }
    return $value.' '.$nameArray[$i];
}
 

Re: If and If

Posted: Tue Sep 08, 2009 4:12 pm
by frozenarmageddon
Thanks cpetercarter, I forgot about the &&, and I see you fixed not just it so thanks a lot <3
Thanks Mark Baker for the function, I think I won't use it though because I need it for only this page momentary which makes defining a function pretty useless at the moment ^^
And thanks tasairis for reminding me that I am using Binaries not SI units lol
You made me want check Wikipedia for it, and made me discover that after Petabytes there are Exabytes, Zettabytes and Yottabytes so special thanks to you too <3