Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts

    Function works once but not again?

    Here's the code...
    Explanation of the problem is at the bottom.

    Class SewerLift.class.php
    PHP Code:
    class SewerLift {    //Retreive the average run hours for pumps
        
    function getAvgHoursRun($id,$pump,$month='') {
            
    $_SESSION["Sewer"] = "";
            if(!empty(
    $month)) {
                
    $monthLimit "AND month='$month'";
            }
            
    $theQuery "SELECT * FROM tcr_lift_station_pumping WHERE station='$id' $monthLimit ORDER BY pump".$pump."_hours DESC";
            
    $get mysql_query($theQuery);
            
    $count 0;
            
            while(
    $row mysql_fetch_array($get)) {
                if(!empty(
    $_SESSION["Sewer"])) {
                    
    $higher $_SESSION["Sewer"];
                    
    $lower $row["pump".$pump."_hours"];
                    
    $total += round(($higher-$lower),2);
                    
    $_SESSION["Sewer"] = $row["pump".$pump."_hours"];
                }
                else {
                    
    $_SESSION["Sewer"] = $row["pump".$pump."_hours"];
                }
                
    $count++;
                
    //Do another loop
                //Get second value (smaller value)
                //Do subtraction
                //Store answer
                //Repeat Loop until finished...
                //take the average.
            
    }
            
    $theCount $count;
            
    $_SESSION["Sewer"] = "";
            return (
    $total/$theCount);
        }

    Contents.php
    PHP Code:
        <tr class=LoopListingLight>
              <td class="LoopListingDark">Avg. East Run Hours</td>
              <td class="LoopListingDark" ><?php echo $Calc->hoursToClock(round($Sewer->getAvgHoursRun(1,1),3)); ?></td>
         </tr>
         <tr class=LoopListingLight>
              <td class="LoopListingLight">Avg. West Run Hours</td>
              <td class="LoopListingLight"><?php echo $Calc->hoursToClock(round($Sewer->getAvgHoursRun(1,2),3)); ?></td>
         </tr>
    Database Table
    http://www.dniwebdesign.com/publicworks/pumping.pdf

    This works fine for the first call but doesn't show anything for the second and halts the rest of the php page. It doesn't matter what pump I call on the second time it doesn't show anything and won't continue with the rest of the page. What is going on?
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Any problem with query on argument change?
    Code:
    $get = mysql_query($theQuery) or die(mysql_error());
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Rather than just say it doesn't produce any output, did you do any debugging?

    Does the code even run as you think it does?

    Echo out some debug statements, echo out the variables content with var_dump(), etc.

  • #4
    New Coder
    Join Date
    Nov 2007
    Posts
    61
    Thanks
    0
    Thanked 6 Times in 6 Posts
    add this to both pages

    PHP Code:
    <?php

    error_reporting
    (E_ALL);

    ini_set('display_errors'true);

    ?>
    this will echo any errors in the php code.

  • Users who have thanked helraizer for this post:

    dniwebdesign (11-12-2007)

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    Why are you using $_SESSION inside that function? You clear the value at the beginning and end of the function, so it seems to serve no purpose that a locally scoped variable couldn't.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #6
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by helraizer View Post
    add this to both pages

    PHP Code:
    <?php

    error_reporting
    (E_ALL);

    ini_set('display_errors'true);

    ?>
    this will echo any errors in the php code.
    The error that outputs is "Fatal error: Call to a member function getAvgHoursRun() on a non-object in /home/.sites/133/site55/web/dniwebdesign/publicworks/ctrl_panel/files/Contents.php on line 442".

    But since the second time I call it, it is exactly the same as the first one, shouldn't it work?
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #7
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    what does the class for Calc look like? (in particular the hoursToClock method.)
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by dniwebdesign View Post
    The error that outputs is "Fatal error: Call to a member function getAvgHoursRun() on a non-object in /home/.sites/133/site55/web/dniwebdesign/publicworks/ctrl_panel/files/Contents.php on line 442".

    But since the second time I call it, it is exactly the same as the first one, shouldn't it work?
    I guess I figured that someone from a web design company, who does not reply to topics that don't have PHP tags around their code, would have thought to turn on error_reporting when debugging...

    You also didn't answer this question:

    Why are you using $_SESSION inside that function? You clear the value at the beginning and end of the function, so it seems to serve no purpose that a locally scoped variable couldn't.

  • #9
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    While I do program php application for clients as well as myself (which this one is for at present time). I don't know everything about PHP. I've tried getting php errors to show on my live server, but with no success, even through the server control panel (there was an option but didn't appear to work). Also, I reply to topics when I feel I can help. While I prefer not to reply to ones without the php tags (as they are a pain to read), I do reply to the odd ones depending on the problem and the severity.

    However... I came for help, not criticism on how I approach things, so...

    The $_SESSION, it is used to store the value last value while looping with the while loop. I figured this was the easiest way. So the first loop would produce a higher number than the next (as that is the way they're ordered), so when it loops a second time the $_SESSION will contain the first value to calculate the hours run. (As the pump hours contains the value taken from a hour clock). If that makes sense... but you may be right, a local variable might work better. Just was late last night when I was coding. :s

    As well, the $Sewer->getAvgHoursRun() method worked fine on my test server, for both East and West pumps. The hoursToClock method works fine on both servers when I run something like:
    PHP Code:
    $Calc->hoursToClock(round(1.6889865),2)); 
    So it doesn't appear to be my $Calc class... but the method is below:



    PHP Code:
        //Function to take hours and turn into a clock format for easier reading of
        //the time.
        //Example: 1.75 hours = 1:45; or 1.50 = 1:30.
        
    function hoursToClock($time) {
            
    //If the $time is equal to "-", then just return the dash.
            //Some entries don't have time stated and dash is the
            //default for no time.
            
    if($time=="-") {
                return 
    "-";
            }
            
    $explode explode(".",$time);
            
    $minutes = ($time*60) % 60;
            
    $hours $explode[0];
            if(
    $minutes<10) {
                
    $minutes "0".$minutes;
            }
            return 
    $hours.":".$minutes;
        } 
    Last edited by dniwebdesign; 11-13-2007 at 02:32 AM.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #10
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    aedrin...
    I changed the $_SESSION to just a local variable. For some wierd reason that seemed to cure my problem. I don't know why but it did. Maybe you can shed some light onto why this cured the problem, or why it would cause that particular problem.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #11
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Since you reset the variable both at the beginning and the end of the function, there was no need to use a SESSION variable (unless of course that was not your intention).

    Changing it to a local variable should not have made a difference. But it's possible that in the process of you updating your code's variable that you "fixed" the part that was causing the error.

  • #12
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    But all I did was change the
    PHP Code:
    $_SESSION["Sewer"
    with
    PHP Code:
    $HighSewer 
    This is how the code has changed:
    PHP Code:
        function getAvgHoursRun($id,$pump,$month='') {
            if(!empty(
    $month)) {
                
    $monthLimit "AND month='$month'";
            }
            else {
                
    $monthLimit "";
            }
            
    $theQuery "SELECT * FROM tcr_lift_station_pumping WHERE station='$id' $monthLimit ORDER BY pump".$pump."_hours DESC";
            
    $get mysql_query($theQuery);
            
    $count = -1;
            
    $total 0;
            while(
    $row mysql_fetch_array($get)) {
                if(!empty(
    $HighSewer)) {
                    
    $higher $HighSewer;
                    
    $lower $row["pump".$pump."_hours"];
                    
    $total += ($higher-$lower);
                    
    //echo $higher."-".$lower."=".($higher-$lower)."<br />";
                    
    $HighSewer $row["pump".$pump."_hours"];
                }
                else {
                    
    $HighSewer $row["pump".$pump."_hours"];
                }
                
    $count++;
                
    //Do another loop
                //Get second value (smaller value)
                //Do subtraction
                //Store answer
                //Repeat Loop until finished...
                //take the average.
            
    }
            
    $theCount $count;
            return (
    $total/$theCount);
        } 
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com


  •  

    Posting Permissions

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