Help with my code.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rub2013
Forum Newbie
Posts: 1
Joined: Fri Nov 01, 2013 9:34 am

Help with my code.

Post by rub2013 »

Greetings

Can anyone assist me. I am trying to display current uploaded HD_space used by users on my website, but my PHP skills are lacking. I want it to brake down in to KB,MB,GB,TB. It is connecting to the database because when I print_r it shows Array ( [0] => Array ( [userID] => 26 [0] => 26 [SUM(fileSize)] => 3068815 [1] => 3068815 ) ) .

Code: Select all



<?php
$sQL           = "SELECT userID, SUM(fileSize) FROM file WHERE userId = ".$Auth->id." ";
$new = $db->getRows($sQL);


foreach($new[1] as $bytes); 


/**
* Converts bytes into human readable file size.
*
* @param string $bytes
* @return string human readable file size (2,87 Мб)
* @author Mogilev Arseny
*/ 



function FileSizeConvert($bytes)
{
    $bytes = floatval($bytes);
        $arBytes = array(
            0 => array(
                "UNIT" => "TB",
                "VALUE" => pow(1024, 4)
            ),
            1 => array(
                "UNIT" => "GB",
                "VALUE" => pow(1024, 3)
            ),
            2 => array(
                "UNIT" => "MB",
                "VALUE" => pow(1024, 2)
            ),
            3 => array(
                "UNIT" => "KB",
                "VALUE" => 1024
            ),
            4 => array(
                "UNIT" => "B",
                "VALUE" => 1
            ),
        );

    foreach($arBytes as $arItem)
    {
        if($bytes >= $arItem["VALUE"])
        {
            $result = $bytes / $arItem["VALUE"];
            $result = str_replace(".", "," , strval(round($result, 2)))." ".$arItem["UNIT"];
            break;
        }
    }
    return $result;
}

echo $result;


print_r($new);

?>



Any help greatly appreciated, I have spend hours trying to work this out. :oops:
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help with my code.

Post by Celauran »

Code: Select all

 foreach($new[1] as $bytes); 
Here's the problem. Try this:

Code: Select all

foreach ($new[1] as $bytes) {
	echo FileSizeConvert($bytes);
}
Post Reply