What's wrong with this?

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by TechAsh, Jul 17, 2008.

Thread Status:
Not open for further replies.
  1. TechAsh

    TechAsh Retired

    Messages:
    5,853
    Likes Received:
    7
    Trophy Points:
    38
    This code works fine:
    PHP:
    function getBandWidth($total) {
            
    $cpanel = new cPanel('*******''******''*****'2083true'x3');
            
    $bu 156.93;
            
    $bt $total;
        
    $bf $bt-$bu;
        
    $percf $bf/$bt*100;
        
    $percu $bu/$bt*100;
        
    $outp round(($percf/100*122)+12);
        
        if (
    $percu >= 90) {
            
    $color "#F00";
        }
        elseif (
    $percu >= 75) {
            
    $color "#F90";
        }
        else {
            
    $color "#000";
        }
        
        echo(
    "<DIV style=\"background-color: " $color "\">\n");
        echo(
    "<IMG src=\"http://famweb.co.cc/includes/images/percentImage.png\" alt=\"\" class=\"percentImage\" style=\"background-position: -".$outp."px 0pt;\"><BR>\n");
        echo(
    "Used: " round("$percu"2) . "%<BR>\n");
        echo(
    "Used: " round("$bu"2) . "MB<BR>\n");
        echo(
    "Free: " round("$percf"2) . "%<BR>\n");
        echo(
    "Free: " round("$bf"2) . "MB<BR>\n");
        echo(
    "</DIV>\n");
    }
    It outputs the amount of bandwidth I've used and got left, as well as percentages. It also outputs a nice progress bar type thing.

    But this code doesn't work:
    PHP:
    function getBandWidth($total) {
    $cpanel = new cPanel('******''****''******'2083true'x3');
    $bu $cpanel->getBandwidthUsed(); //<-- This is the only bit that's changed.
    $bt $total;
    $bf $bt-$bu;
    $percf $bf/$bt*100;
    $percu $bu/$bt*100;
    $outp round(($percf/100*122)+12);

    if (
    $percu >= 90) {
    $color "#F00";
        }
    elseif (
    $percu >= 75) {
    $color "#F90";
        }
    else {
    $color "#000";
        }

    echo(
    "<DIV style=\"background-color: " $color "\">\n");
    echo(
    "<IMG src=\"http://famweb.co.cc/includes/images/percentImage.png\" alt=\"\" class=\"percentImage\" style=\"background-position: -".$outp."px 0pt;\"><BR>\n");
    echo(
    "Used: " round("$percu"2) . "%<BR>\n");
    echo(
    "Used: " round("$bu"2) . "MB<BR>\n");
    echo(
    "Free: " round("$percf"2) . "%<BR>\n");
    echo(
    "Free: " round("$bf"2) . "MB<BR>\n");
    echo(
    "</DIV>\n");
    }                                                                                                                
    This should do exactly the same except it gets the total used bandwidth from a function. However this doesn't work it says that I've not used any of my bandwidth. I've checked that the function is outputting the correct data.

    Any suggestions?
    It's probably something really obvious, but I'm only a beginner with PHP.
     
    Last edited: Jul 17, 2008
  2. marshian

    marshian New Member

    Messages:
    526
    Likes Received:
    9
    Trophy Points:
    0
    Where did you get that class cPanel? I'm not used to object-oriented programming in php but it's most likely an easy-to-solve problem.
     
  3. TechAsh

    TechAsh Retired

    Messages:
    5,853
    Likes Received:
    7
    Trophy Points:
    38
    That class is a cPanel API. It just allows you to collect stats etc. from cPanel.
     
  4. marshian

    marshian New Member

    Messages:
    526
    Likes Received:
    9
    Trophy Points:
    0
    Hmm, I've found exactly the same line of code on another site, and it should work there, and also according to you the function outputs the correct result, so I suggest you try to cast it to an integer and see if it helps.

    PHP:
    function getBandWidth($total) {
    $cpanel = new cPanel('******''****''******'2083true'x3');
    $bu = (int) $cpanel->getBandwidthUsed(); //To cast stuff to an integer, put (int) in front of it
    $bt $total;
    $bf $bt-$bu;
    $percf $bf/$bt*100;
    $percu $bu/$bt*100;
    $outp round(($percf/100*122)+12);

    if (
    $percu >= 90) {
    $color "#F00";
        }
    elseif (
    $percu >= 75) {
    $color "#F90";
        }
    else {
    $color "#000";
        }

    echo(
    "<DIV style=\"background-color: " $color "\">\n");
    echo(
    "<IMG src=\"http://famweb.co.cc/includes/images/percentImage.png\" alt=\"\" class=\"percentImage\" style=\"background-position: -".$outp."px 0pt;\"><BR>\n");
    echo(
    "Used: " round("$percu"2) . "%<BR>\n");
    echo(
    "Used: " round("$bu"2) . "MB<BR>\n");
    echo(
    "Free: " round("$percf"2) . "%<BR>\n");
    echo(
    "Free: " round("$bf"2) . "MB<BR>\n");
    echo(
    "</DIV>\n");
    }  
    EDIT:
    omg, I already found this page on google o_O
     
    Last edited: Jul 17, 2008
  5. DefecTalisman

    DefecTalisman Community Advocate Community Support

    Messages:
    4,148
    Likes Received:
    5
    Trophy Points:
    38
    ok, I modded that class for you right ?
    PM me and I will see what I can work out for you !

    Try going to line 297 of the class and uncomment out the
    PHP:
    // settype($value[1], $type);
    Also, sorry for the multi posts.
    PHP:
    function getBandWidth($total) {
    $cpanel = new cPanel('******''****''******'2083true'x3');
    $bu $cpanel->getBandwidthUsed();
    $percf = (($total-$bu)/$total)*100;
    $percu $bu/$total*100;
    $outp round(($percf/100*122)+12);

    if (
    $percu >= 90) {
    $color "#F00";
        }
    elseif (
    $percu >= 75) {
    $color "#F90";
        }
    else {
    $color "#000";
        }



    $string "<DIV style=\"background-color: " $color "\">\n";
    $string .= "<IMG src=\"http://famweb.co.cc/includes/images/percentImage.png\" alt=\"\" class=\"percentImage\" style=\"background-position: -".$outp."px 0pt;\"><BR>\n";
    $string .= "Used: " round("$percu"2) . "%<BR>\n";
    $string .= "Used: " round("$bu"2) . "MB<BR>\n";
    $string .= "Free: " round("$percf"2) . "%<BR>\n";
    $string .= "Free: " round("$total-$bu"2) . "MB<BR>\n";
    $string .= "</DIV>\n");

    return 
    $string;
    }
    cleaned the function up a little. Rather return the output and then echo it.
     
    Last edited: Jul 17, 2008
  6. DefecTalisman

    DefecTalisman Community Advocate Community Support

    Messages:
    4,148
    Likes Received:
    5
    Trophy Points:
    38
    ok, and to acompany the cpanelAIP, I just hard codded a gd image for the disk space usage.

    EDITED

    [​IMG]
    [noparse]http://www.defectalisman.com/class/statBar/index.php?width=500&height=30&percent=60&backcolor=255.255.255[/noparse]

    [​IMG]
    [noparse]http://www.defectalisman.com/class/statBar/index.php?width=350&height=30&used=10&max=18&backcolor=255.255.255[/noparse]


    [​IMG]
    [noparse]http://www.defectalisman.com/class/statBar/index.php?width=350&height=30&used=10&max=20&type=disk&backcolor=255.255.255[/noparse]


    [​IMG]
    [noparse]http://www.defectalisman.com/class/statBar/index.php?width=350&height=20&used=10&max=20&type=disk&backcolor=255.255.255&font_size=10[/noparse]

    [​IMG]
    [noparse]http://www.defectalisman.com/class/statBar/index.php?width=100&height=20&font_size=12&backcolor=255.255.255&used=10&max=15&type=disk[/noparse]

    Its still a little buggy, so please don't laugh.

    EDIT
    Finished(had a few hiccups).
    http://www.defectalisman.com/examples/statBar.rar
     
    Last edited: Jul 18, 2008
  7. TechAsh

    TechAsh Retired

    Messages:
    5,853
    Likes Received:
    7
    Trophy Points:
    38
    Right I've tried putting (int) before the cPanel function and that didn't work, and I've tried un-commenting that line, but that didn't work either.

    I'll send you a PM Defec.
     
  8. woiwky

    woiwky New Member

    Messages:
    390
    Likes Received:
    0
    Trophy Points:
    0
    It would actually be (float) or (double) to maintain accuracy. But php autocasts anyway so that can't be the problem.

    What do you mean by you checked that the function's output is correct? You mean you checked that $cpanel->getBandwidthUsed() returns the correct value? I can't see how it could return the correct value but then still say 0 bandwidth used. Please double check the return value by echoing $bu's value right after it's set to $cpanel->getBandwidthUsed().

    Also, is this on x10? If so, then unless you have a different cpanel than my account this code won't work because this regex won't match anything:

    PHP:
    preg_match('/' $key '<\/td>' "\n" '               <td class="index2">(.*)<\/td>/'$this->HTTP->getData('index.html'), $value);
    The string it's searching for doesn't exist anywhere on the index page. Easy way to check, go to the index page of your cpanel and view the html source. Then search for 'Bandwidth (this month)'. If it doesn't come up, that's the problem. In that case, the regex just needs to change a bit.
     
  9. DefecTalisman

    DefecTalisman Community Advocate Community Support

    Messages:
    4,148
    Likes Received:
    5
    Trophy Points:
    38
    Ahh... thats all great and good. But the funny thing in the cpanel aip code I echoed out the source of the index straight after acquiring it and it is not the same as what I see when going to view the source from a browser ?
     
  10. TechAsh

    TechAsh Retired

    Messages:
    5,853
    Likes Received:
    7
    Trophy Points:
    38
    Yes I echoed the output from $bu, and it outputs exactly what I expect.
     
  11. VPmase

    VPmase New Member

    Messages:
    914
    Likes Received:
    0
    Trophy Points:
    0
    Can I see the "getBandwidthUsed()" function code? That's probably where the problem is.
     
  12. TechAsh

    TechAsh Retired

    Messages:
    5,853
    Likes Received:
    7
    Trophy Points:
    38
    Sure here it is:
    PHP:
    /**
    * Get bandwidth usage
    *
    * Returns the amount of bandwidth used this month in megabytes.
    * @return float
    */
    function getBandwidthUsed()
    {
    return 
    $this->parseIndex2('Monthly Bandwidth Transfer''float');
    }
    and here is parseIndex2():
    PHP:
    function parseIndex2($key$type 'string')
    {
    $value = array();
    preg_match('/' $key '([^MB]*)/'$this->HTTP->getData('index.html'), $value);
    $tmp explode('</div></div>',$value[1]);
    $tmp2 explode('/td>',$tmp[1]);
    $tmp3 explode('/',$tmp2[1]);
    $value[1] = $tmp3[0];
    // settype($value[1], $type);
    return $value[1];
    }
     
    Last edited: Jul 18, 2008
  13. VPmase

    VPmase New Member

    Messages:
    914
    Likes Received:
    0
    Trophy Points:
    0
    Ok well if you only changed that one line of code for the first function, have you tried just calling getBandidthUsed() by its self and seeing what happens? Aka do a little debugging :p
     
  14. woiwky

    woiwky New Member

    Messages:
    390
    Likes Received:
    0
    Trophy Points:
    0
    I output the source of index.html from the class too and it looks the same(aside from the inclusion of the HTTP headers at the top). Very strange.

    Also, TechAsh, can you attach the complete cpanel class? I noticed from the code you just showed that the one I got is not the one you're using. It's already modified to use the correct regex.

    And yes, VPmase, she said she output the value from getBandwidthUsed() and it was correct.
     
  15. TechAsh

    TechAsh Retired

    Messages:
    5,853
    Likes Received:
    7
    Trophy Points:
    38
    Well I've tried this:
    PHP:
    echo $cpanel->getBandwidthUsed();
    and that works.

    EDIT:
    I've think I've found the problem!! The function is actually outputting the following:
    Which is why the output looked right in a browser, it was only when I looked in the source that I noticed this.
    So, how would I fix this?

    EDIT2:
    I'm a 'he' not a 'she'.
     
    Last edited: Jul 18, 2008
  16. DefecTalisman

    DefecTalisman Community Advocate Community Support

    Messages:
    4,148
    Likes Received:
    5
    Trophy Points:
    38
    There see seems to be a bit of a tag in the out put, but browser doesn't display the tag. If I print it in a image it shows
    PHP:
    %3Ctd%20%20align=" left="" class="truncate">
    still in the out put. Thats why setting the type to int/float and so on doesn't work.

    You saying the class you have is allready modified to work or the one TecAsh is already modded. If so then yes I modded the class to kinda work.
     
  17. DefecTalisman

    DefecTalisman Community Advocate Community Support

    Messages:
    4,148
    Likes Received:
    5
    Trophy Points:
    38
    There we go nailed the son of a (uhm).

    Ok change the function to this
    PHP:
    function parseIndex2($key$type 'string')
        {
            
    $value = array();
            
    preg_match('/' $key '([^MB]*)/'$this->HTTP->getData('index.html'), $value);
            
    $tmp explode('</div></div>',$value[1]);
            
    $tmp2 explode('/td>',$tmp[1]);
            
    $tmp3 explode('/',$tmp2[1]);
            
            
                
    $tmp4 explode('>',$tmp3[0]);
                
    $tmp3[0] = $tmp4[1];
            
            
    $value[1] = $tmp3[0];
            
    settype($value[1], $type);
            return 
    $value[1];
        }

    :p
    Free Space
    [​IMG]
    Bandwidth
    [​IMG]

    Really odd that the disk space was working and not the bandwidth. I got the same problem from both
     
    Last edited: Jul 18, 2008
  18. woiwky

    woiwky New Member

    Messages:
    390
    Likes Received:
    0
    Trophy Points:
    0
    Wow you're a guy. I completely forgot Ashley can be a guy's name too. Sorry about that :p

    Anyway, nice job. I was completely blind to that. Just use strip_tags() on the return value or in parseIndex2() on the value before it's returned. That should fix it.
     
  19. DefecTalisman

    DefecTalisman Community Advocate Community Support

    Messages:
    4,148
    Likes Received:
    5
    Trophy Points:
    38
    if you really want you can add the strip tags to it:
    PHP:
    function parseIndex2($key$type 'string')
        {
            
    $value = array();
            
    preg_match('/' $key '([^MB]*)/'$this->HTTP->getData('index.html'), $value);
            
    $tmp explode('</div></div>',$value[1]);
            
    $tmp2 explode('/td>',$tmp[1]);
            
    $tmp3 explode('/',$tmp2[1]);
            
    $tmp4 explode('>',$tmp3[0]);
            
    $tmp3[0] = $tmp4[1];
            
    $value[1] = $tmp3[0];
            
    strip_tags($value[1]);
            
    settype($value[1], $type);
            return 
    $value[1];
        }
    but removing the new lines I added before will break it. The tag doesn't have a closing tag so it doesn't strip it.
     
  20. woiwky

    woiwky New Member

    Messages:
    390
    Likes Received:
    0
    Trophy Points:
    0
    I don't believe strip_tags() needs a closing tag to strip an html tag. I just tested it even and it works fine.
     
Thread Status:
Not open for further replies.

Share This Page