【问题标题】:In PHP MySQL Sum total nos of rows based on search results在 PHP MySQL 中,根据搜索结果求和总行数
【发布时间】:2019-10-05 03:28:22
【问题描述】:

在PHP MySQL中,我做了手机商城系统,用户买卖手机,买卖金额以表格形式插入数据库。

我正在从视图中的表中检索所有数据并计算买卖价格的总数。

我也是根据手机IMEI、账单号、型号等不同参数进行搜索的。

但搜索后它会正确显示结果,但我想根据他们的搜索显示移动价格总额。

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

【问题讨论】:

标签: php sql mysqli cumulative-sum


【解决方案1】:

在SO上搜索后,我找到了以下答案:-

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

【讨论】:

    猜你喜欢
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多