<html>
    <head>
        
    </head>
    <body>
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="excel_file" >
            <input type="submit" name="submit" value="Import" >
        </form>
        
    <?php
    include'connection.php';
    //ob_start();
    use SimpleExcel\SimpleExcel;
    require_once('../faisalman-simple-excel-php-ea5326d/src/SimpleExcel/SimpleExcel.php');
    

    if(isset($_POST['submit']))
    {
        if(move_uploaded_file($_FILES['excel_file']['tmp_name'], $_FILES['excel_file']['name']))
        {
            $excel = new SimpleExcel('CSV');
            $excel->parser->loadFile($_FILES['excel_file']['name']);
            
            $data_txn =  $excel->parser->getField();
            $array_length = count($data_txn);
            
            for($i=0; $i<$array_length; $i++)
            {   
                $rech_amt = $data_txn[$i][2];
                $lapu_bal = $data_txn[$i][3];
                $order_id = $data_txn[$i][4];
                $status   = $data_txn[$i][7];
                $lapu_no  = $data_txn[$i][11];
                
                $diff=0;
                if($status=='success' || $status=='SUCCESS')
                {
                    $quRech="SELECT * FROM recharge WHERE Id<$order_id AND Status='SUCCESS' AND LapuNo='$lapu_no' ORDER BY Id DESC";
                    $reRech=mysqli_query($con,$quRech);
                    $dataRech=mysqli_fetch_array($reRech);
                    $lapuOldBal = $dataRech['ApiBal'];
                    $remain_bal = $lapuOldBal-$rech_amt;
                    $diff = $lapu_bal-$remain_bal;
                    
                    $update="UPDATE recharge SET LapuNo='$lapu_no', LapuOldBal='$lapuOldBal', ApiBal='$lapu_bal', Diff='$diff' WHERE Id='$order_id'";
                    mysqli_query($con,$update);
                }
                
                
                
            }
            
            echo "All Updated";
            
            /*
            
            
                print_r ($data_txn[142][4]."<br>");
                print_r ($data_txn[142][11]."<br>");
                echo "<pre>";
                    print_r ($data_txn);
                echo "</pre>";
            
            
            /*
            $excel->parser->getCell(1, 1);
            $excel->convertTo('JSON');
            $excel->writer->addRow(array('add', 'another', 'row'));
            $excel->writer->saveFile('example');
            */
            
        }
        else
        {
            echo "failed";
        }
    }
    //ob_end_clean();
?>

    </body>
</html>