...

View Full Version : Function works once but not again?



dniwebdesign
11-12-2007, 08:33 AM
Here's the code...
Explanation of the problem is at the bottom.

Class SewerLift.class.php

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

<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?

abduraooft
11-12-2007, 10:23 AM
Any problem with query on argument change?

$get = mysql_query($theQuery) or die(mysql_error());

aedrin
11-12-2007, 03:56 PM
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.

helraizer
11-12-2007, 04:46 PM
add this to both pages


<?php

error_reporting(E_ALL);

ini_set('display_errors', true);

?>

this will echo any errors in the php code.

GJay
11-12-2007, 05:07 PM
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.

dniwebdesign
11-12-2007, 06:32 PM
add this to both pages


<?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?

GJay
11-12-2007, 07:51 PM
what does the class for Calc look like? (in particular the hoursToClock method.)

aedrin
11-12-2007, 09:10 PM
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.

dniwebdesign
11-13-2007, 02:24 AM
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:
$Calc->hoursToClock(round(1.6889865),2)); So it doesn't appear to be my $Calc class... but the method is below:





//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;
}

dniwebdesign
11-13-2007, 02:33 AM
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.

aedrin
11-13-2007, 03:57 PM
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.

dniwebdesign
11-14-2007, 04:10 AM
But all I did was change the
$_SESSION["Sewer"] with
$HighSewer

This is how the code has changed:

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);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum