Wednesday, February 26, 2014

sub query to get data from mysql with php

I want to get data from mysql data by filtering different columns and want to compare each other. 
I have following data in database.


I want to get General Shift of year 2010, 2011, 2012, 2013 and 2014 and later compare them means sum of all. and same for Shifts and common. and also want to get by designation. I am using following php script get year wise data but can't compare it. 
please help me to solve this problem.



<?php include ('header3.php');
if (isset($_GET['year']))
{
$year = $_GET['year'];
$shift = $_GET['shift'];
}
?>


<table width="100%" id="table">
<thead><tr><td colspan="10">
<h1> SANCTION STRENGTH REPORT </h1>
<h2> <?php echo $shift; ?> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Year-<?php echo $year; ?> </h2>
</td></tr>
<tr bgcolor="#D9D9D9" height="20" align="center"><th width="40"> ID </th>
<th width="200"> Name </th><th width="150"> Designation </th><th>ON <br /> Roll </th><th> ON <br /> Duty</th><th> Present <br /> Rate </th>
<th>Present <br /> Amount </th><th> Increment</th><th> Proposed <br /> Rate</th><th> Proposed <br /> Amount</th>
</tr></thead>
  <tbody>

<?php

  $gt_on_roll = 0;
$gt_on_duty = 0;
$gt_pre_amount = 0;
$gt_increment = 0;
$gt_pro_amount = 0;
$id1=1;
$id2=0;

  include ('config.php');
$query="SELECT DISTINCT department FROM strength WHERE year='$year' and shift='$shift' ORDER BY dpt_srt ASC";
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();
$i=0;
while ($i < $num) {
 $department = mysql_result($result,$i,"department");
 echo "<tr><th colspan='10'>".$department."</th></tr>";
  $t_on_roll = 0;
$t_on_duty = 0;
$t_pre_amount = 0;
$t_increment = 0;
$t_pro_amount = 0;
$id = 1;
mysql_connect($mysql_host,$mysql_user,$mysql_password)
or die(mysql_error());

mysql_select_db($mysql_database) or die(mysql_error());
$data = mysql_query("SELECT * FROM strength WHERE department='$department' and shift='$shift' and year='$year' ORDER BY dsgn_srt DESC, id ASC")
 or die(mysql_error());
 while($info = mysql_fetch_array( $data ))
 {

$name =$info['name'];
$designation =$info['designation'];
$on_roll =$info['on_roll'];
$on_duty =$info['on_duty'];
$pre_rate =$info['pre_rate'];
$pre_amount = $on_roll*$pre_rate;
$increment =$info['increment'];
$remarks =$info['remarks'];
$pro_rate = $pre_rate+$increment;
$pro_amount = $on_roll*$pro_rate;

echo "<tr><td> $id </td><td> $name </td>";
echo "<td> $designation </td>";
echo "<td> $on_roll</td><td> $on_duty</td><td> $pre_rate</td><td> $pre_amount</td>";
echo "<td> $increment </td><td> $pro_rate</td><td> $pro_amount</td></tr>";
if($remarks!="")
{ echo "<tr><td colspan='10' style='color:red;'>Note: $name - $remarks </td><tr>"; }

$t_on_roll = $t_on_roll+$on_roll;
$t_on_duty = $t_on_duty+$on_duty;
$t_pre_amount = $t_pre_amount+$pre_amount;
$t_increment = $t_increment+$increment;
$t_pro_amount = $t_pro_amount+$pro_amount;

$gt_on_roll = $gt_on_roll+$on_roll;
$gt_on_duty = $gt_on_duty+$on_duty;
$gt_pre_amount = $gt_pre_amount+$pre_amount;
$gt_increment = $gt_increment+$increment;
$gt_pro_amount = $gt_pro_amount+$pro_amount;

 $id++;
 $id1++;
 }
$t_pre_amount = number_format($t_pre_amount);
$t_pro_amount = number_format($t_pro_amount);
  echo "<tr><th colspan='3'> TOTAL >> </th>";
echo "<th> $t_on_roll</th><th> $t_on_duty</th><th> </th><th> $t_pre_amount</th>";
echo "<th> </th><th>  </th><th> $t_pro_amount</th></tr>";
 $i++;
 $id2++;
}
  $gt_pre_amount = number_format($gt_pre_amount);
$gt_pro_amount = number_format($gt_pro_amount);
echo "<tr><td colspan='10'></td></tr><tr><th colspan='3'> GRAND TOTAL ======>>>>>>> </th>";
echo "<th> $gt_on_roll</th><th> $gt_on_duty</th><th> </th><th> $gt_pre_amount</th>";
echo "<th> </th><th>  </th><th> $gt_pro_amount</th></tr>";
?>

    </tbody>
</table>
<?php include ('footer1.php'); ?>