In PHP MySQL , I have made mobile shop system , in which user buy and sell the mobile and amount of buy & sell is inserted in database by form.
i am retrieve all the data from table in view and count total no of buy and sell price.
also i am seaching based on different parameters like mobile IMEI, Bill no, Model no,etc.
but after searching it will show results correctly, but i want to show the mobile price total according to their search.
if(isset($_POST['submit'])){
$a = $_POST['userInput']; //user enter text for search
$sql = "select * from mobile where mob_bill='$a' or mob_imei='$a' or mob_model='$a'";
$result = mysqli_query($conn,$sql);
$output="";
while($row=mysqli_fetch_array($result)){
$bill = $row['mob_bill'];
$imei = $row['mob_imei'];
$model = $row['mob_model'];
$output.="<tr>
<td>$bill</td>
<td>$imei</td>
<td>$model</td>
</tr>";
}
}
else{
$sql = "select *, Sum(buy_payment) AS buyTotal, Sum(sell_payment) AS sellTotal from mobile";
$result = mysqli_query($conn,$sql);
$output1="";
while($row=mysqli_fetch_array($result)){
$bill = $row['mob_bill'];
$imei = $row['mob_imei'];
$model = $row['mob_model'];
$buy_payment=$row['buyTotal'];
$sell_payment=$row['sellTotal'];
$output1.="<tr>
<td>$bill</td>
<td>$imei</td>
<td>$model</td>
<td>$buy_payment</td>
<td>$sell_payment</td>
</tr>";
}
}
<?php echo $outout; ?>
<?php echo $outout1; ?>
After Searching on SO, I found the answer below:-
if(isset($_POST['submit'])){
$a = $_POST['userInput']; //user enter text for search
$query2 = "select sum(buy_price) as buyPrice, sum(sell_price) as sellPrice from mobile where mob_bill='$a' or mob_imei='$a' or mob_model='$a' or mob_status='$a'";
$result2 = mysqli_query($conn,$query2);
$output2="";
while($row2=mysqli_fetch_array($result2)){
$buyPrice = $row2['buyPrice'];
$sellPrice = $row2['sellPrice'];
}
$sql = "select * from mobile where mob_bill='$a' or mob_imei='$a' or mob_model='$a' or mob_status='$a'";
$result = mysqli_query($conn,$sql);
$output="";
while($row=mysqli_fetch_array($result)){
$bill = $row['mob_bill'];
$imei = $row['mob_imei'];
$model = $row['mob_model'];
$output.="<tr>
<td>$bill</td>
<td>$imei</td>
<td>$model</td>
</tr>";
}
}
else{
}
<?php echo $outout; ?>
<?php echo $buyPrice; ?>
<?php echo $sellPrice; ?>
来源:https://stackoverflow.com/questions/56192489/in-php-mysql-sum-total-nos-of-rows-based-on-search-results