<?php  
 include('db_con.php');  
 $output['rows'] = '';  
 $area = $_POST['area'];
 $sm_id = $_POST['sm_id'];
 $sql = "select a.acc_no, a.acc_name name,balance from accounts a where a.acc_type = 'DEBTOR' and a.sm_id='".$sm_id."' and a.area = '".$area."' order by name asc";
 $statement = $connect->prepare($sql);
 $statement->execute();
 $result = $statement->fetchAll();
 $rows = $statement->rowCount();
 $tot_amount = 0;
 $balance = 0;
 $count = 0;
 $tot_balance = 0;
 if($rows > 0)  
 {  
	 
      foreach($result as $row)
      {  
          $count++;

         $balance = $row['balance'];
         $tot_balance = $tot_balance + $balance;
         $acc_no = $row['acc_no'];
         $amountid = "amount".$count;
           $output['rows'] .= '  
                <tr>  
                <td>'.$count.'</td>
                     <td><input type="hidden" name="acc_no[]"  class="form-control" type="text" data-srno="'.$count.'" value="'.$acc_no.'" readOnly>
                     '.$row["name"].'</td>  
                     <td class="rightside">'.number_format($balance,0).'</td>  
                     <td><input name="amount[]" type="number" step="any" id="'.$amountid.'" data-srno="'.$count.'" class="form-control rightside amount"></td>  
                     
                     
                     
                </tr>  
           ';  
      
      }  
      $output['count'] = $count;
      $tot_balance = number_format($tot_balance,2);
      $output['tot_balance'] = $tot_balance;
  
 }  
 
 echo json_encode($output);  
 ?>