Create following table in your "my_database" .Here we are going to get average weekly production of a good produced in a company and balance between production and sale.
Codes for above table creation is bellow.
CREATE TABLE IF NOT EXISTS `computer_sale_january` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`weeks` varchar(40) NOT NULL,
`annual_target` varchar(40) NOT NULL,
`production` varchar(40) NOT NULL,
`sale` varchar(40) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
PHP codes to display average and balance from the table above is as bellow.
<?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>Sale</strong></td> <td " align="center" "><strong>Balance</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ $balance=$rows['production']-$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 $rows['sale']; ?></td> <td align="center" bgcolor="#FFFFFF"><?php echo $balance; ?></td> </tr> <?php } mysql_close(); ?> <?php mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $query = "SELECT weeks,AVG(annual_target), AVG(production) , AVG(sale) FROM computer_sale_january"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ ?> <tr bgcolor="#ddeef5" align="center"><td colspan=2>Average</td><td><?php echo $row['AVG(annual_target)'];?></td><td><?php echo $row['AVG(production)'];?></td><td><?php echo $row['AVG(sale)'];?></td><td> </td> </tr> <?php } mysql_close(); ?> </table> |
Final table looks as bellow.
ID | Weeks | Annual Target | production | Sale | Balance |
4 | 1st | 4000 | 1000 | 500 | 500 |
5 | 2nd | 5000 | 800 | 450 | 350 |
6 | 3rd | 6000 | 2000 | 1000 | 1000 |
7 | 4th | 10000 | 2000 | 1500 | 500 |
Average | 6250 | 1450 | 862.5 |
No comments:
Post a Comment