Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: if..else loops

  1. #1
    New Coder
    Join Date
    Jul 2007
    Location
    Masai, Johor, Malaysia
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile if..else loops

    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 Code:
    <?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.

  • #2
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Hi,

    Firstly, please could you indent your code!

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

    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?

  • #3
    New Coder
    Join Date
    Jul 2007
    Location
    Masai, Johor, Malaysia
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    last row

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

  • #4
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ?

  • #5
    New Coder
    Join Date
    Jul 2007
    Location
    Masai, Johor, Malaysia
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    discription

    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:
    PHP Code:
    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..

  • #6
    New Coder
    Join Date
    Jul 2007
    Location
    Masai, Johor, Malaysia
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Here the looping code

    PHP Code:
    <?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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •