简体   繁体   中英

Subtract payment from bill in php, when payment is not available in the Mysql table

Hi my below condition is not working properly,when billpay field is not have the "payment" value. Then first "if" is now showing any data of $102 in the table, its just showing blank cell.

if($f1022 ==0){
   echo $f102;  
}
 else {
   echo $fpending = $f102 - $f1022;
}

I have also tried as below, but result is same,

if(empty($f1022)){
  echo $f102;   
}
else {
  echo $fpending = $f102 - $f1022;
}

AND

     if($f1022 ==0){
       echo $f102;  
       }
       else {
     echo $fpending = $f102 - $f1022;
      }

在此处输入图片说明

Here below full script with html table.

<html>
<head>
<style>
table, td, th {    
border: 1px solid #ddd;
text-align: left;
 }

table {
border-collapse: collapse;
 }

 th, td {
padding: 15px;
}
 </style>
 </head>
<body> 
<table width="700px" id="workweek" class="wwtable"> 
 <tbody> 
 <tr>
 <th>Supplier</th>
 <th>Bill</th>
 <th>Payment</th>
 <th>Due</th>
 </tr>

<?php
error_reporting(1);
$conn = mysqli_connect("127.0.0.1","root","","mehrazin");

  $detailss81s = "SELECT *, SUM(acamount) AS sum FROM account WHERE billpay 
   = 
'bill' GROUP BY factsuply ";
  $details_results81s = mysqli_query($conn,$detailss81s)
   or die ( "Couldn't get Products: ".mysqli_error($conn) );
   while ( $det_row = mysqli_fetch_array ( $details_results81s ) )
     {//===1st
     $f101 = $det_row[ 'factsuply' ];
   $f102 = $det_row[ 'sum' ];   
     ?>
    <tr>
<td width="400px"><?php echo "$f101"; ?></td>
<td width="100px"><?php echo "$f102"; ?></td>
   <?php
   $detailss81ss = "SELECT *, SUM(acamount) AS sum FROM account WHERE 
 factsuply 
= '$f101' AND billpay = 'payment' GROUP BY factsuply ";
$details_results81ss = mysqli_query($conn,$detailss81ss)
or die ( "Couldn't get Products: ".mysqli_error($conn) );
while ( $det_rows = mysqli_fetch_array ( $details_results81ss ) )
    {//===2nd
  $f1022 = $det_rows[ 'sum' ];  

  ?>
 <td width="100px"><?php  echo "$f1022"; ?></td>
  <td width="100px"><?php 

  if($f1022 ==0){
  echo $f102;   
   }
   else {
  echo $fpending = $f102 - $f1022;
   }

  ?></td>

  <?php  
  }//===2nd 
  }//===1st
   ?>
 </tr>  

 </tbody>
</table>
</body> 
</html>

I think your query is the root cause of the problem. As it stands now, the WHERE clause may be discarding certain factsuply groups entirely which do not match. One way around this is to use conditional aggregation on the entire table:

SELECT
    factsuply,
    SUM(CASE WHEN billpay = 'bill' THEN acamount ELSE 0 END) AS sum 
FROM account 
GROUP BY factsuply;

By the way, doing SELECT * along with GROUP BY is generally a bad thing. In your particular case, MySQL was tolerating it, and it even does not appear to causing your problem. But you should know that this query would not even run on most other databases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM