[RESOLVED] Need code help.
Results 1 to 6 of 6

Thread: [RESOLVED] Need code help.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3

    resolved [RESOLVED] Need code help.

    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 ) ) .

    PHP Code:

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


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

    function 
    FileSizeConvert($bytes)
    {
        
    $bytes floatval($bytes);
            
    $arBytes = array(
                
    => array(
                    
    "UNIT" => "TB",
                    
    "VALUE" => pow(10244)
                ),
                
    => array(
                    
    "UNIT" => "GB",
                    
    "VALUE" => pow(10243)
                ),
                
    => array(
                    
    "UNIT" => "MB",
                    
    "VALUE" => pow(10242)
                ),
                
    => array(
                    
    "UNIT" => "KB",
                    
    "VALUE" => 1024
                
    ),
                
    => array(
                    
    "UNIT" => "B",
                    
    "VALUE" => 1
                
    ),
            );

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

    echo 
    $result;


    print_r($new);

    ?>

    Any help greatly appreciated, I have spent hours trying to work this out.

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    Considering that you copied verbatim (with no source credit...) this user-contributed note, have you tried actually calling the function and seeing if it does what you need?

    EDIT: Also note that this foreach() loop:
    PHP Code:
    foreach($new[1] as $bytes); 
    not only doesn't make any sense ($new[1] isn't an array), but the semicolon at the end means it has no actual body (so even if $new[1] was an array, PHP would loop over it and do... nothing). Furthermore, you later try to echo a variable called $result, yet no such variable exists.

    You might want to visit the manual page for the function construct in PHP... or you might want to start with some very basic PHP tutorials.
    Last edited by bradgrafelman; 11-01-2013 at 12:50 PM.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    My apologies, I did not mean to removed credit from anybody. I downloaded a folder with lots of functions, but the Author is showing //@*********. (I didn't find this appropriate for posting.) As for my bad code, I really have no clue where I have gone wrong. PHP is too complex for my tiny brain, I was hoping someone could show me where I am going wrong.

  4. #4
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,729
    Here are a couple of suggestions for this snippet above:

    1. Foreach loops are built like this:

    PHP Code:
    foreach ($array as $individual_variable) {

       
    do_something($individual_variable);


    2. A function doesn't need to be defined inside a loop. I would move it to the bottom of the script. The first line of your function is:

    PHP Code:
    function FileSizeConvert($bytes)  { //moved up from the line below; I don't like the author's style manual, although it is common and acceptable. 
    The last two lines of the function are:
    PHP Code:
        return $result

    So, the first thing I would do is move this function to the very top of the script (it could, optionally, be moved to the very bottom).

    This would leave this:

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

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

    echo 
    $result

    print_r($new); 
    As you can see, the loop isn't constructed properly (not constructed at all, really, as bradgrafelman points out).

    I don't know the content/structure that $db->getRows() returns, but I'm guessing what you want is something like this:

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

    foreach(
    $new as $data_array) {

       echo 
    "User: ".$data_array[0]."<br>";
       echo 
    "Space Used: ".FileSizeConvert($data_array[1]);
       echo 
    "<br>";

    }

    //debugging use only
    //print_r($new); 
    HTH,
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thank you both, I see where I was going wrong and it is all working now.

  6. #6
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,729
    Quote Originally Posted by rub2013 View Post
    Thank you both, I see where I was going wrong and it is all working now.
    Yay!

    Please consider using the "Mark Thread Resolved" link under "Thread Tools" at the top right of the page; and welcome to PHPBuilder!
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •