Butterbean Posted January 9, 2015 Share Posted January 9, 2015 (edited) I'm being told I have an undefined variable. Similar to sumUsagekWH (in the code below) there are other placeholders called sumUsageKHW, sumUsageWKWp, sumUsageWKWi and so on. Each script of code looks similar to below where the 'totalUsage' is added into sumUsageKHW, sumUsageKHE and so on. All references are highlighted red below to make it easier to follow. So, if I am only using these sumUsagesXXXX's as placeholders for the following post.... <input name="electric_charge_total" type="hidden" id="electric_charge_total" value="<?php echo number_format($basic_charge + ($sumUsageKWH * $energy_charge) + ($sumUsageWKWp * $peak_rate) + ($sumUsageWKWi * $intermediate_rate) + ( ($sumUsageWKW+$sumUsageWEW) * $base_rate),"2"); ?>" align="right"/> how to I define them in PHP? Based on the code below, they are only used as a way to introduce totalUsage ( it looks silly the way it was done, but later the code will be expanded so it makes sense). What I mean is that totalUsage is being entered (in the below case) into sumUsageKWH from the $sumUsageKWH += $row[totalUsage] Notice: Undefined variable: sumUsageKWH in C:\xampp1\htdocs\Utrack\invoice.php on line 73 <?php $sql = ";WITH TOTAL_KWH_WINTER AS ( SELECT CONVERT(VARCHAR(10),cdate,111)AS trans_date, datepart(hh, cdate) as trans_hour, comm_id, MIN((total_energy_a+total_energy_b+total_energy_c)/100) AS minUsage, MAX((total_energy_a+total_energy_b+total_energy_c)/100) AS maxUsage, meter_multiplier FROM [radiogates].[dbo].[purge_data] LEFT OUTER JOIN [radiogates].[dbo].[ops_invoice] on [radiogates].[dbo].[purge_data].[comm_id] = [radiogates].[dbo].[ops_invoice].[meter_id] where comm_id='$comm_id'and meter_multiplier is not null group by comm_id, CONVERT(VARCHAR(10),cdate,111), datepart(hh, cdate), meter_multiplier ) SELECT *, datepart(weekday, trans_date) as trans_date_day, datepart(month, trans_date) as trans_date_month, ((maxUsage - minUsage)*meter_multiplier) as totalUsage FROM TOTAL_KWH_WINTER where datepart(weekday, trans_date) IN ('1', '2', '3', '4', '5', '6', '7') AND DATEPART(MONTH, trans_date) IN ('10','11','12','1','2','3','4') and trans_date BETWEEN '$startdate2 00:00:01' AND '$enddate2 24:00:00' "; $query = sqlsrv_query($conn, $sql); if ($query === false){ exit("<pre>".print_r(sqlsrv_errors(), true));}while ($row = sqlsrv_fetch_array($query)) { $sumUsageKWH += $row[totalUsage];}sqlsrv_free_stmt($query);?> Edited January 9, 2015 by Butterbean Quote Link to comment https://forums.phpfreaks.com/topic/293768-why-am-i-being-asked-to-define-a-descriptor/ Share on other sites More sharing options...
Solution kicken Posted January 9, 2015 Solution Share Posted January 9, 2015 while ($row = sqlsrv_fetch_array($query)){ $sumUsageKWH += $row[totalUsage]; } Code like that will throw an undefined variable notice because it is the same as: $sumUsageKWH = $sumUsageKWH + $row[totalUsage]; Notice how you are trying to read the variables current value, add the total usage, then assign the result? On the first iteration of the loop $sumUsageKWH does not exist so you get the notice. To fix the notice, just assign the variable before the loop to an initial value. 0 would work well in this case. $sumUsageKWH=0; while ($row = sqlsrv_fetch_array($query)){ $sumUsageKWH += $row['totalUsage']; } Note also that your array index needs to be quoted when it is a string (as shown above). Not doing so will cause another notice error about an undefined constant. Quote Link to comment https://forums.phpfreaks.com/topic/293768-why-am-i-being-asked-to-define-a-descriptor/#findComment-1502226 Share on other sites More sharing options...
Butterbean Posted January 9, 2015 Author Share Posted January 9, 2015 Thank you. I have take a full year of coding in school and have gotten more out of this forum in 1 month than sitting in a class. I appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/293768-why-am-i-being-asked-to-define-a-descriptor/#findComment-1502227 Share on other sites More sharing options...
Butterbean Posted January 9, 2015 Author Share Posted January 9, 2015 (edited) I am still getting this error. $sumUsageKWH=0; while ($row = sqlsrv_fetch_array($query)) {$sumUsageKWH += $row['totalUsage'];} Parse error: syntax error, unexpected '=' in C:\xampp1\htdocs\Utrack\invoice.php on line 75 Edited January 9, 2015 by Butterbean Quote Link to comment https://forums.phpfreaks.com/topic/293768-why-am-i-being-asked-to-define-a-descriptor/#findComment-1502228 Share on other sites More sharing options...
Butterbean Posted January 9, 2015 Author Share Posted January 9, 2015 Sorry, I botched that. It works now. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/293768-why-am-i-being-asked-to-define-a-descriptor/#findComment-1502229 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.