Search code examples
phpmysqlaccounting

Trial Balance Update accounts


Am looking to update my chartofaccount to get an trial balance here what i have in the table of chartofaccount

Id             parent         Debit          Credit

1
10              1              
101             10
1011            101                          30
101121231       1011          40
101134121       1011          20

i need the table to look like this

Id             parent         Debit          Credit

1                             60             30
10              1             60             30
101             10            60             30
1011            101           60             30
101121231       1011          40
101134121       1011          20

The idea is that i need to sum the account Id with respect to the parent

here what am using but its just updating 1 record

    $select2="SELECT sum(debit),sum(credit),parent
                    FROM chartofaccount
                    group by parent
                    ORDER BY parent DESC";
    $run2=mysql_query($select2,$con);
    if(!$run2)die("ERROR22".mysql_error());

    for($counter=0;$row1=mysql_fetch_array($run2);$counter++){

        $parentid=$row1[2];
        $update3="UPDATE chartofaccount
                    SET debit='$row1[0]',
                        credit='$row1[1]'
                        WHERE Id='$parentid'";
        $run3=mysql_query($update3,$con);
        if(!$run3)die("ERROR1".mysql_error());

    }

Solution

  • $select2="SELECT Distinct(parent) FROM chartofaccount
                                ORDER BY parent DESC";
                $run2=mysql_query($select2,$con);
                if(!$run2)die("ERROR22".mysql_error());
    
                for($counter=0;$row1=mysql_fetch_assoc($run2);$counter++){
    
                    $parentid=$row1['parent'];
    
                    $select3="SELECT sum(debit),sum(credit),parent
                                FROM chartofaccount
                                WHERE parent='$parentid'
                                group by parent";
                    $run3=mysql_query($select3,$con);
                    if(!$run3)die("ERROR22".mysql_error());
    
                    for($counter=0;$row2=mysql_fetch_array($run3);$counter++){
    
                            $parentidz=$row2[2];
    
                                $update3="UPDATE chartofaccount
                                            SET debit='$row2[0]',
                                                credit='$row2[1]'
                                                WHERE Id='$parentidz'";
                                $run4=mysql_query($update3,$con);
                                if(!$run4)die("ERROR1".mysql_error());
                    }
                }