In PHP MySQL Sum total nos of rows based on search results

こ雲淡風輕ζ 提交于 2019-12-02 23:02:32

问题


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; ?>

回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!