Jump to content

PHP Consumption Calculation


karenn1

Recommended Posts

Hey everyone!

 

I need some help please. I'm running a page that displays a table with data pulled in from a database. This is my query:

 

$sql = "SELECT Log_Date, Log_Reading, Channel, Device_Code, Zone_Code FROM datalog
		WHERE Zone_Code = '".$_REQUEST['zone']."'
		AND Log_Date BETWEEN '".$_REQUEST['day_from']."' AND '".$_REQUEST['day_to']."'
		AND (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan1_Reference='".$_REQUEST['reference']."') AND Channel=1)   
		OR  (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan2_Reference='".$_REQUEST['reference']."') AND Channel=2)  
		OR  (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan3_Reference='".$_REQUEST['reference']."') AND Channel=3)  
		OR  (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan4_Reference='".$_REQUEST['reference']."') AND Channel=4) 
		ORDER BY Channel, Log_Date";
$result = mysql_query($sql);

 

This is the php code getting the data:

 

<?php
while ($rs = mysql_fetch_array($result))	{
			$prevtotal = $i - $prevtotal;
	?>
		<tr>
              <td height="25" style="padding-left:5px; padding-right:2px;"><div class="style1 style8">
                <?= ucwords($rs["Log_Date"]); ?>
              </div></td>
		  <td height="25" align="center" style="padding-left:5px; padding-right:2px;"><div class="style1 style8">
		    <?= ucwords($rs["Channel"]); ?></div></td>
              <td height="25" align="center" class="style1" style="padding-left:5px; padding-right:2px;"><span class="style8">
                <?= ucwords($rs["Zone_Code"]); ?>
              </span> </td>
              <td height="25" align="center" class="style1" style="padding-left:5px; padding-right:2px;"><div><span class="style8">
                <?= ucwords($rs["Device_Code"]); ?>
              </span> </div></td>
              <td height="25" align="center" class="style1" style="padding-left:5px; padding-right:2px;"><div><span class="style8">
              </span> </div></td>
              <td align="center" class="style1" style="padding-left:5px; padding-right:2px;"> </td>
            </tr>
            <?php $i++; } ?>

 

This returns my table with date and log reading for that date and looks like this:

 

Date      Reading      Consumption

12 Jan        39                0

13 Jan        54              15

14 Jan        59                5

15 Jan        63                4

16 Jan        83              20

 

What I'm trying to achieve is the reading in the consumption table. I have to calculate this as it isn't in the database. It's basically each value minus the one previous to it.

 

Can someone please help me on this? I don't know how to do this in PHP.

 

 

Thanks!

 

Karen

 

Link to comment
https://forums.phpfreaks.com/topic/144034-php-consumption-calculation/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.