...

View Full Version : if..else loops



NiZAM
07-21-2007, 05:20 PM
I need somebody to repair the looping..It only loops 2 times. I wanted it to loops at least 5 times and display only the last row for each employee. All the data i get from database HPTRX tables.


<?php
$var_i=1;
$EE1 = " "; $EE2= " ";
$T_OT1=0; $T_OT2=0; $T_OT3=0; $T_OT4=0; $T_OT5=0;
$OT1=0; $OT2=0; $OT3=0; $OT4=0; $OT5=0;
while (!$rs_hptrx->EOF) {

$TT = $rs_hptrx->Fields('TRX_CODE');
$AMT = $rs_hptrx->Fields('tot_ot');
$T_AMT = $T_AMT + $rs_hptrx->Fields('tot_ot');

if ( $TT == 'OT1' ) {$OT1 = $AMT; $T_OT1 = $T_OT1 + $AMT;}
else if ( $TT == 'OT2' ) {$OT2 = $AMT; $T_OT2 = $T_OT2 + $AMT;}
else if ( $TT == 'OT3' ) {$OT3 = $AMT; $T_OT3 = $T_OT3 + $AMT;}
else if ( $TT == 'OT4' ) {$OT4 = $AMT; $T_OT4 = $T_OT4 + $AMT;}
else if ( $TT == 'OT5' ) {$OT5 = $AMT; $T_OT5 = $T_OT5 + $AMT;}

$var_i3++;

if (!($EE1 != $EE2)){

?>
<tr class="12">
<td width="3%"><?php echo $var_i ?></td>
<td width="15%" height="46" ><?php echo $rs_hptrx->Fields('emp_Id'); ?></td>
<td width="16%"><?php echo $rs_hptrx->Fields('emp_Name'); ?></td>
<td width="16%"><?php echo $OT1; ?></td>
<td width="7%"><?php echo $OT2; ?></td>
<td width="7%"><?php echo $OT3; ?></td>
<td width="7%"><?php echo $OT4; ?></td>
<td width="7%"><?php echo $OT5; ?></td>
<td width="13%"><?php echo $T_AMT; ?></td>
</tr>
<?php
//$T_AMT = 0;
$var_i++;
$EE1 = $rs_hptrx->Fields('emp_Id');

//$OT1=0; $OT2=0; $OT3=0; $OT4=0; $OT5=0;
}
$rs_hptrx->MoveNext();

$EE2 = $rs_hptrx->Fields('emp_Id');

} // while not eof rs_hptrx
?>

<tr class="">
<td height="2" colspan="1" ></td>
</tr>
<tr class="12">
<td width="3%"><?php echo $var_i -1 ?></td>
<td height="16" colspan="2" >GRAND TOTAL </td>
<td width="7%"><?php echo $T_OT1; ?></td>
<td width="7%"><?php echo $T_OT2; ?></td>
<td width="7%"><?php echo $T_OT3; ?></td>
<td width="7%"><?php echo $T_OT4; ?></td>
<td width="7%"><?php echo $T_OT5; ?></td>
<td width="13%"></td>
</tr>

Thanks in advance.

mlse
07-22-2007, 03:53 PM
Hi,

Firstly, please could you indent your code! :eek:

Secondly, I don't see how that code can work at all - where is $rs_hptrx assigned prior to the while loop?! :confused:

Thirdly, assuming that $rs_hptrx is infact a database record set resource, surely the number of iterations of the loop would depend upon the database and not upon the PHP code?

NiZAM
07-23-2007, 09:55 AM
I know it hard to understand... but what I want to display just the last row for each employee inside the loop. The record are group by EMP_NO and TRX_DATE..if I display the data directly from the database, it will display in one one column and the same emp_no will be repeated. I want it to be in one row for each category of OT_TYPE. ..:D

paulyu
07-24-2007, 04:47 AM
Hi NiZAM,

I would like to help. It would be easier if you could briefly state what data you get from your DB, what kind of calculation you would like to do, how you would like to display the results ?

NiZAM
07-24-2007, 07:09 AM
Okay..
I get the data from HPTRX table content EMP_NO, TRX_DATE, TRX_TYPE, TRX_CODE, AMOUNT. this table keep the data about the employee overtime paid amount. when I get the data directly, it will display in one column with the same emp_no repeated.

What I want to do is to display the total amount for each emp_no with different TRX_CODE in one row and different column. Something like this:


EMP_NO TRX_DATE OT1 OT2 OT3 OT4 OT5 TOTAL
5421 2007/05/31 110.20 210.00 0 0 54.00 374.20
6541 2007/05/31 200.00 100.00 30.00 0 0 330.00
.......

and so on..

The loops can do the calculation but it seems it has lost some amount.. I really need you to help with it.

Sorry for the trouble..

NiZAM
07-24-2007, 07:51 AM
<?php
$var_i=1;
$EE1 = " "; $EE2= " ";
$T_OT1=0; $T_OT2=0; $T_OT3=0; $T_OT4=0; $T_OT5=0;
$OT1=0; $OT2=0; $OT3=0; $OT4=0; $OT5=0;
while (!$rs_hptrx->EOF) {

$TT = $rs_hptrx->Fields('TRX_CODE');
$AMT = $rs_hptrx->Fields('tot_ot');
$T_AMT = $T_AMT + $rs_hptrx->Fields('tot_ot');

if ( $TT == 'OT1' ) {$OT1 = $AMT; $T_OT1 = $T_OT1 + $AMT;}
else if ( $TT == 'OT2' ) {$OT2 = $AMT; $T_OT2 = $T_OT2 + $AMT;}
else if ( $TT == 'OT3' ) {$OT3 = $AMT; $T_OT3 = $T_OT3 + $AMT;}
else if ( $TT == 'OT4' ) {$OT4 = $AMT; $T_OT4 = $T_OT4 + $AMT;}
else if ( $TT == 'OT5' ) {$OT5 = $AMT; $T_OT5 = $T_OT5 + $AMT;}

$var_i3++;

if ($EE1 != $EE2){

?>
<tr class="12">
<td width="3%"><?php echo $var_i ?></td>
<td width="15%" height="46" ><?php echo $rs_hptrx->Fields('emp_Id'); ?></td>
<td width="16%"><?php echo $rs_hptrx->Fields('emp_Name'); ?></td>
<td width="16%"><?php echo $OT1; ?></td>
<td width="7%"><?php echo $OT2; ?></td>
<td width="7%"><?php echo $OT3; ?></td>
<td width="7%"><?php echo $OT4; ?></td>
<td width="7%"><?php echo $OT5; ?></td>
<td width="13%"><?php echo $T_AMT; ?></td>
</tr>
<?php
$T_AMT = 0;
$OT1=0; $OT2=0; $OT3=0; $OT4=0; $OT5=0;
$var_i++;
$EE1 = $rs_hptrx->Fields('emp_Id');
}
$rs_hptrx->MoveNext();
$EE2 = $rs_hptrx->Fields('emp_Id');

} // while not eof rs_hptrx
?>

<tr class="">
<td height="2" colspan="1" ></td>
</tr>
<tr class="12">
<td width="3%"><?php echo $var_i -1 ?></td>
<td height="16" colspan="2" >GRAND TOTAL </td>
<td width="7%"><?php echo $T_OT1; ?></td>
<td width="7%"><?php echo $T_OT2; ?></td>
<td width="7%"><?php echo $T_OT3; ?></td>
<td width="7%"><?php echo $T_OT4; ?></td>
<td width="7%"><?php echo $T_OT5; ?></td>
<td width="13%"></td>
</tr>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum