flemingmike Posted February 4, 2011 Share Posted February 4, 2011 hello, is anybody able to help me with why i am getting an undefined variable "total" on line 109? <?php include '../auth5.php'; include '../config.php'; include '../javascript.php'; include 'style.php'; echo "<br />"; echo "<table border='1' style='border-collapse: collapse' bordercolorlight='#000000' bordercolordark='#000000' width='98%' align='center'>"; echo "<tr><td width='100%' colspan='7' align='center'><b>Unbilled Work</b></td></tr>"; echo "<tr> <th align='center'>Date</th> <th align='center'>Client</th> <th align='center'>Tech</th> <th align='center'>Service Report</th> <th align='center'>Hours Billed</th> <th align='center'>Total</th> <th align='center'>Parts Used</th> </tr>"; $result = mysql_query("SELECT * FROM timesheets WHERE status = 3 AND billable = 1 ORDER BY date, starttime"); while($row = mysql_fetch_array($result)) { $id=$row['id']; $tech=$row['tech']; $date=$row['date']; $date1=date( 'M d', strtotime($date) ); $date2=date( 'D M j, Y', strtotime($date) ); $starttime=$row['starttime']; $starttime1=date( 'g:i a', strtotime($starttime) ); $esttime=$row['esttime']; $esttime1=date( 'g:i a', strtotime("$esttime hours, $starttime") ); $endtime=$row['endtime']; $endtime1=date( 'g:i a', strtotime($endtime) ); $client=$row['client']; $description=$row['description']; $description=nl2br($description); $status=$row['status']; $report=$row['report']; //$report=nl2br($report); $billable=$row['billable']; $remote=$row['remote']; $parts=$row['parts']; if($endtime == "01:11:00") { $tbilled3 = ""; $endtime1 = ""; } elseif(empty($endtime)) { $tbilled3 = ""; $endtime1 = ""; } else { $log_in_time_string = strtotime($starttime); $log_out_time_string = strtotime($endtime); $difference_in_seconds = ($log_out_time_string - $log_in_time_string); $tbilled = ($difference_in_seconds / 3600); if($tbilled < 0) { $tbilled1 = $tbilled + 24; } else { $tbilled1=$tbilled; } $tbilled2 = number_format(round($tbilled1*4)/4,2); if($remote == 1) { if($tbilled2 < 1.5) { $tbilled3 = "2"; } else { $tbilled3 = $tbilled2 + 0.5; } } else { $tbilled3 = $tbilled2; } } $result5 = mysql_query("SELECT * FROM customers WHERE id = '$client'"); while($row5 = mysql_fetch_array($result5)) { $clientname=$row5['name']; $rate=$row5['rate']; } $result6 = mysql_query("SELECT * FROM staff WHERE id = '$tech'"); while($row6 = mysql_fetch_array($result6)) { $tech=$row6['name']; } $nlength = strlen($report); $nremove = 30 - $nlength; $report1 = substr($report, 0, $nremove); $jobbill=$tbilled3*$rate; $jobbill = number_format($jobbill, 2, '.', ','); $replacement = array(","); $pnone = array(""); $pplain = str_replace($replacement, $pnone, $jobbill); $total += $pplain; $total1 = number_format($total, 2, '.', ','); echo "<tr>"; echo "<td align='center'>" . $date2 . "</td>"; echo "<td align='center'>" . $clientname . "</td>"; echo "<td align='center'>" . $tech . "</td>"; echo "<td>" . $report1 . "</td>"; echo "<td align='center'>" . $tbilled3 . "</td>"; echo "<td align='center'>$" . $jobbill . "</td>"; echo "</tr>"; } echo "<tr>"; echo "<td colspan='5' align='right'><b>Total :</td>"; echo "<td align='right'><b>$" . $total1 . "</td>"; echo "<tdcolspan='2'></td>"; echo "</tr>"; echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/226730-undefined-variale-help/ Share on other sites More sharing options...
Pikachu2000 Posted February 4, 2011 Share Posted February 4, 2011 Since $total is undefined when you attempt to use it here --> $total += $pplain; <--, it's throwing an error. Quote Link to comment https://forums.phpfreaks.com/topic/226730-undefined-variale-help/#findComment-1170052 Share on other sites More sharing options...
flemingmike Posted February 4, 2011 Author Share Posted February 4, 2011 so a $total=0 at the start should solve i imagine? Quote Link to comment https://forums.phpfreaks.com/topic/226730-undefined-variale-help/#findComment-1170056 Share on other sites More sharing options...
Pikachu2000 Posted February 4, 2011 Share Posted February 4, 2011 Or just use the assignment operator instead. $total = $pplain; Quote Link to comment https://forums.phpfreaks.com/topic/226730-undefined-variale-help/#findComment-1170059 Share on other sites More sharing options...
flemingmike Posted February 4, 2011 Author Share Posted February 4, 2011 where would i put that? Quote Link to comment https://forums.phpfreaks.com/topic/226730-undefined-variale-help/#findComment-1170060 Share on other sites More sharing options...
flemingmike Posted February 5, 2011 Author Share Posted February 5, 2011 im trying to add all of the $pplain together Quote Link to comment https://forums.phpfreaks.com/topic/226730-undefined-variale-help/#findComment-1170100 Share on other sites More sharing options...
Pikachu2000 Posted February 5, 2011 Share Posted February 5, 2011 In that case, yes. Initialize the variable as zero before the loop. Quote Link to comment https://forums.phpfreaks.com/topic/226730-undefined-variale-help/#findComment-1170102 Share on other sites More sharing options...
flemingmike Posted February 5, 2011 Author Share Posted February 5, 2011 so in this case, echo "<td align='center'>"; echo "<table border='0' style='border-collapse: collapse' bordercolorlight='#000000' bordercolordark='#000000' width='100%' align='center'>"; $result7 = mysql_query("SELECT * FROM parts WHERE timesheet = '$id'"); while($row7 = mysql_fetch_array($result7)) { $partdescription=$row7['description']; $partprice=$row7['price']; $partprice = number_format($partprice, 2, '.', ','); $replacementp = array(","); $pnonep = array(""); $pplainp = str_replace($replacementp, $pnonep, $partprice); $totalp += $pplainp; $total1p = number_format($totalp, 2, '.', ','); echo "<tr>"; echo "<td>" . $partdescription . "</td>"; echo "<td align='center' width='30'>$" . $partprice . "</td>"; echo "</tr>"; } echo "</table>"; echo "</td>"; id want it to look like echo "<td align='center'>"; echo "<table border='0' style='border-collapse: collapse' bordercolorlight='#000000' bordercolordark='#000000' width='100%' align='center'>"; $result7 = mysql_query("SELECT * FROM parts WHERE timesheet = '$id'"); $totalp=0; while($row7 = mysql_fetch_array($result7)) { $partdescription=$row7['description']; $partprice=$row7['price']; $partprice = number_format($partprice, 2, '.', ','); $replacementp = array(","); $pnonep = array(""); $pplainp = str_replace($replacementp, $pnonep, $partprice); $totalp += $pplainp; $total1p = number_format($totalp, 2, '.', ','); echo "<tr>"; echo "<td>" . $partdescription . "</td>"; echo "<td align='center' width='30'>$" . $partprice . "</td>"; echo "</tr>"; } echo "</table>"; echo "</td>"; Quote Link to comment https://forums.phpfreaks.com/topic/226730-undefined-variale-help/#findComment-1170109 Share on other sites More sharing options...
flemingmike Posted February 5, 2011 Author Share Posted February 5, 2011 when i do it that way, it gets rid of the error, but it isnt adding up the values. it is only showing me that $total1p is equal to the newest entry in the table. Quote Link to comment https://forums.phpfreaks.com/topic/226730-undefined-variale-help/#findComment-1170111 Share on other sites More sharing options...
Pikachu2000 Posted February 5, 2011 Share Posted February 5, 2011 What happens between the end of that code and where you echo the value? Quote Link to comment https://forums.phpfreaks.com/topic/226730-undefined-variale-help/#findComment-1170126 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.