0

I'm trying to "sum" $total in loop.

This is the full code:

foreach ( $array_permall as $perm ) {
    if($lastcheck != "1" && $checkoutlet != 1){
        //izin gagal
        echo "2";
        exit();
    }else{
        $total = 0;
        $sqlIns = "BEGIN TRAN ";

        if($lastcheck == "1"){
            $sqlIns .=  "INSERT INTO T_Absen_Permission (NIK,Reason,Date,AddBy,AddDate) VALUES('".$nik."','PERMISSION ALL','".$tglco."','".$User."',getdate());";

            $sqlIns .=  "INSERT INTO T_Absen Values('".$nik."','".$long."','".$lat."','".$rid."','".$oid."','CHECK OUT','".$tglco."','PERMISSION By ".$User."');";
        }

        if($checkoutlet == 1){
            $sqlIns .=  "INSERT INTO T_Absen_Permission (NIK,Reason,Date,AddBy,AddDate,Note) VALUES('".$nik."','PERMISSION ALL',dbo.GetLastDay(getdate(),'".$nik."'),'".$User."',getdate(),'Permission Outlet');";
        }

        if(mssql_query($sqlIns)){
            mssql_query("COMMIT");
            $total++;
        }else{
            mssql_query("ROLLBACK");
            echo "5";
        }
    }
    echo $total;
}

But the result is always 11111. I want the result is like : 1+1+1+1+1 = 5

I want use Final Result 5 to make alert in JavaScript.

How can I do it with PHP?

Zhorov
  • 28,486
  • 6
  • 27
  • 52
Kirito
  • 1

1 Answers1

1

On each iteration you're setting $total = 0 so after each iteration and $total++; it gets incremented to 1 that's why you are getting 1 echoed each time loop runs, Initialize your $total=0 variable outside the loop, Hope it helps.

Waqas Mustafa
  • 192
  • 1
  • 7