-5

I have a table of data im importing, the contents of a particular row looks like this...

500, 1000, 2000, 4000, 5000, 8000, 10000, 12000, UNLIMITED

Each number represents Megabytes where as 8000 is 8000mb or 8gb I need a function which will knock off the last 3 zero's of the number if it is greater than 999 then add "GB" to the end of the number if not over 999 add "MB" to the end of the original number. so i end up with for each cell e.g. 500MB or 8GB - also I need the function to just echo "unlimited" in lower case if the case is "UNLIMITED" this would have been easy last year but im really rusty with my PHP at the minute and need a refresher course. Can you help me?

heres the code I have already wrote it doesn't work though...

function gigaplex($data) {
if ($data = 300) {
    echo "300MB";
} elseif ($data = 500) {
    echo "500MB";
} elseif ($data = 1000) {
    echo "1GB";
} elseif ($data = 2000) {
    echo "2GB";
} elseif ($data = 3000) {
    echo "3GB";
} elseif ($data = 4000) {
    echo "4GB";
} elseif ($data = 5000) {
    echo "5GB";
} elseif ($data = 8000) {
    echo "8GB";
} elseif ($data = 10000) {
    echo "10GB";
} elseif ($data = 12000) {
    echo "12GB";
} else {
    echo "unlimited";
}

}

mitchelangelo
  • 851
  • 4
  • 16
  • 42
  • 2
    I'd suggest you hire a programmer for that or try it yourself. – Ulrich Eckhardt Feb 04 '16 at 21:57
  • I will be happy to answer this for you if you clean up your formatting so it's not confusing for other people to follow. EDIT: Where does the "Switch" come in to play? – jaggedsoft Feb 04 '16 at 21:59
  • You should post some code which represents your effort. If you still have problems then we might help you. Noone here will do your work for free. Besides, what you want to do is pretty easy and is solveable with very basic functions. So stop being lazy and learn if you need to solve this. – Steini Feb 04 '16 at 22:00
  • 2
    StackOverflow is not for "Please give me teh codez", if you try it, and you can't get it to work, post a question here showing what you have tried and we can help you. @NextLocal Just make sure you don't give too much away and keep the OP from a learning experience. Steini, let's be a bit nicer. – Ruan Mendes Feb 04 '16 at 22:00
  • This is a good example of making zero effort and expecting the answer. – ggdx Feb 04 '16 at 22:01
  • Possible duplicate of [Format bytes to kilobytes, megabytes, gigabytes](http://stackoverflow.com/questions/2510434/format-bytes-to-kilobytes-megabytes-gigabytes) – jaggedsoft Feb 04 '16 at 22:05
  • First Google search result for `php convert to mb` http://www.wordinn.com/solution/223/php-format-file-size-gb-mb-kb-or-bytes-etc – jaggedsoft Feb 04 '16 at 22:07
  • 1
    Look at the manual. http://php.net/manual/en/function.filesize.php See the first `User Contributed Notes` and modify as needed. – chris85 Feb 04 '16 at 22:07
  • Im not being lazy I have already wrote something but its long and ugly – mitchelangelo Feb 04 '16 at 22:12
  • function gigaplex($data) { if ($data = "UNLIMITED") { echo "unlimited"; } elseif ($data = "300") { echo "300MB" } elseif ($data = "500") { echo "500MB" } elseif ($data = "1000") { echo "1GB" } elseif ($data = "2000") { echo "2GB" } elseif ($data = "3000") { echo "3GB" } elseif ($data = "4000") { echo "4GB" } elseif ($data = "5000") { echo "5GB" } elseif ($data = "8000") { echo "8GB" } elseif ($data = "10000") { echo "10GB" } else ($data = "12000") { echo "12GB" } } – mitchelangelo Feb 04 '16 at 22:12
  • and it doesn't cover me if the data changes ? – mitchelangelo Feb 04 '16 at 22:13
  • there is a problem with my code also it returns unlimited for a 10000 value??? – mitchelangelo Feb 04 '16 at 22:23
  • think about *=* vs *==* –  Feb 04 '16 at 22:33
  • i have pasted the code into my question that i just wrote but it wont work for some reason just spits out 300MB even though the value im passing to it is 10000 can anyone see a problem? – mitchelangelo Feb 04 '16 at 22:34
  • Dagon that worked :) – mitchelangelo Feb 04 '16 at 22:36
  • it would be cool if I could say ask - if $data is 3 numbers ### or something echo them 3 numbers ### and add mb or if $data is more than 3 numbers #### delete the final three numbers and add a GB at the end but its too complicated at the minute because ive not been coding for a few years so ill just make a buff if else statement haha thanks Dagon – mitchelangelo Feb 04 '16 at 22:43
  • that did not occur to me until you just said it @Barmar – mitchelangelo Feb 04 '16 at 22:54

3 Answers3

0

Even if I don't think it's a good idea to check if $data is greater than 999 and cut the last 3 characters from your variable, this is exactly what you want:

function gigaplex($data) {
    if($data > 999) {
         $data = substr($data, 0, -3);
         $data .= "GB";
    } else if($data == 'UNLIMITED') {
         return 'UNLIMITED';
    } else {
         $data .= "MB";
    }
    return $data;
}

I would prefer this one:

function gigaplex($data) {
    if($data >= 1000) {
         $data /= 1000;
         return $data . "GB";
    } else if($data == 'UNLIMITED') {
         return 'UNLIMITED';
    } else {
         return $data . "MB";
    }
}
bpoiss
  • 13,673
  • 3
  • 35
  • 49
0

Divide the number by 1,000, and then round it down.

fuunction gigaplex($data) {
    if (!is_numeric($data)) {
        return $data;
    } elseif ($data >= 1000) {
        return floor($data/1000) . "GB";
    } else {
        return $data . "MB";
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

This one works perfectly..

function gigaplex($data) {
   if($data == "UNLIMITED") {
     echo 'unlimited';
   } else if ($data >= 1000) {
     $data /= 1000;
     echo $data."GB";
   } else {
     echo $data."MB";
   }
}
mitchelangelo
  • 851
  • 4
  • 16
  • 42