Jump to content

Why am I being asked to define a descriptor?


Butterbean
Go to solution Solved by kicken,

Recommended Posts

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 by Butterbean
Link to comment
Share on other sites

  • Solution

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.
Link to comment
Share on other sites

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 by Butterbean
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.