Monday, June 4, 2012

Cumulative total and percentage php

Cumulative total and percentage php

This lessen we can use the table which used in Average and balance php called "computer_sale_january". Create following php file and view the result.

<?php
$host="localhost";
$username="root";
$password="";
$db_name="my_database";
$tbl_name="computer_sale_january";


mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";

$result=mysql_query($sql);
?>
<table  align="center"  bgcolor="#15475c">
<tr bgcolor="#ddeef5">
<td align="center" ><strong>ID</strong></td>
<td align="center" "><strong>Weeks</strong></td>
<td  align="center" "><strong>Annual Target</strong></td>
<td align="center" "><strong>production</strong></td>
<td align="center" "><strong>production</br> Percentage</strong></td>
<td align="center" "><strong>Cumulative</br> production</strong></td>
<td " align="center" "><strong>Sale</strong></td>
<td " align="center" "><strong>Cumulative</br> Sale</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
$percentage=($rows['production']/$rows['annual_target'])*100;
$percentage=sprintf("%01.2f",$percentage);
$cumulative=$cumulative+$rows['production'];
$cumulative2=$cumulative2+$rows['sale'];
?>
<tr bgcolor="#ddeef5">
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['weeks']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['annual_target']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['production']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $percentage; ?>%</td>
<td align="center" bgcolor="#FFFFFF"><?php echo $cumulative; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['sale']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $cumulative2; ?></td>
</tr>

<?php

}
mysql_close();
?>

</table>

Resulting final  table will be as bellow.

ID Weeks Annual Target production production
Percentage
Cumulative
production
Sale Cumulative
Sale
4 1st 4000 1000 25.00% 1000 500 500
5 2nd 5000 800 16.00% 1800 450 950
6 3rd 6000 2000 33.33% 3800 1000 1950
7 4th 10000 2000 20.00% 5800 1500 3450

No comments:

Post a Comment