FOR循环和SQL查询

时间:2016-03-14 06:38:29

标签: php sql loops

我正在做每日销售报告,显示从早上8点到晚上10点的每一小时的总销售额。

时间很简单,只需

for ($x = 8; $x <= 22; $x++)

然后总金额将来自SQL:

$sql = "SELECT DATEPART(hh,LastUpdateTime), SUM(TotalAmount) AS Total FROM Tickets WHERE DATEPART(hh,LastUpdateTime)=$x GROUP BY DATEPART(hh,LastUpdateTime)";

如果该小时没有任何销售,金额将为0.所以我的代码是:

<?php
include 'go.php';
for ($x = 8; $x <= 22; $x++) {
    $money =0;
    echo "['".$x.":00', ".$money."],<br>";
    $sql = "SELECT DATEPART(hh,LastUpdateTime), SUM(TotalAmount) AS Total FROM Tickets WHERE DATEPART(hh,LastUpdateTime)=$x GROUP BY DATEPART(hh,LastUpdateTime)";
    $stmt = sqlsrv_query( $conn, $sql );
        if( $stmt === false) {
            die( print_r( sqlsrv_errors(), true) );
        }
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
            $money = $row['Total'];
            echo "['".$x.":00', ".$money."],<br>";
        }
    //echo $x."<br>";
} 
sqlsrv_free_stmt( $stmt);
?>

但请帮助我,销售时间出现两次(看看13:00,15:00,17:00,......)

['8:00', 0],
['9:00', 0],
['10:00', 0],
['11:00', 0],
['12:00', 0],
['13:00', 0],
['13:00', 22.05],
['14:00', 0],
['15:00', 0],
['15:00', 23.95],
['16:00', 0],
['17:00', 0],
['17:00', 47.45],
['18:00', 0],
['18:00', 71.50],
['19:00', 0],
['20:00', 0],
['21:00', 0],
['22:00', 0],

我应该如何更改代码以获得完美的结果:

['8:00', 0],
['9:00', 0],
['10:00', 0],
['11:00', 0],
['12:00', 0],
['13:00', 22.05],
['14:00', 0],
['15:00', 23.95],
['16:00', 0],
['17:00', 47.45],
['18:00', 71.50],
['19:00', 0],
['20:00', 0],
['21:00', 0],
['22:00', 0],

非常感谢!!!

5 个答案:

答案 0 :(得分:0)

你要显示两次时间和金钱:

尝试将代码更改为:

<?php
include 'go.php';
for ($x = 8; $x <= 22; $x++) {
    $money =0;
//Remove or comment this line 
//        echo "['".$x.":00', ".$money."],<br>";
        $sql = "SELECT DATEPART(hh,LastUpdateTime), SUM(TotalAmount) AS Total FROM Tickets WHERE DATEPART(hh,LastUpdateTime)=$x GROUP BY DATEPART(hh,LastUpdateTime)";
        $stmt = sqlsrv_query( $conn, $sql );
            if( $stmt === false) {
                die( print_r( sqlsrv_errors(), true) );
            }
            while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
                if ($row['Total']) {
                    $money = $row['Total'];
                }
                echo "['".$x.":00', ".$money."],<br>";
            }
        //echo $x."<br>";
    } 
    sqlsrv_free_stmt( $stmt);
    ?>

答案 1 :(得分:0)

我认为你需要删除你的第一个echo语句:

for ($x = 8; $x <= 22; $x++) {
    $money =0;
    //Remove this one
    echo "['".$x.":00', ".$money."],<br>";
...
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
        $money = $row['Total'];
        //Leave this one
        echo "['".$x.":00', ".$money."],<br>";
    }

答案 2 :(得分:0)

当您开始循环此行时,请始终打印

echo "['".$x.":00', ".$money."],<br>";

无物质查询是否有记录。查询查找某些记录时,必须覆盖此行。我提炼你查询。

<?php
$i =0; $arr = array();
for ($x = 8; $x <= 22; $x++) {
    $money =0;
    $arr[$i] = "['".$x.":00', ".$money."],<br>";
    $sql = "SELECT DATEPART(hh,LastUpdateTime), SUM(TotalAmount) AS Total FROM Tickets WHERE DATEPART(hh,LastUpdateTime)=$x GROUP BY DATEPART(hh,LastUpdateTime)";
    $stmt = sqlsrv_query( $conn, $sql );
        if( $stmt === false) {
            die( print_r( sqlsrv_errors(), true) );
        }
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
            $money = $row['Total'];
            $arr[$i] = "['".$x.":00', ".$money."],<br>";
        }
    //echo $x."<br>";
$i++;
} 
echo implode(" ", $arr);
sqlsrv_free_stmt( $stmt);
?>

答案 3 :(得分:0)

我弄明白了这个问题。我将while更改为if else 代码现在运作良好

<?php
include 'go.php';
for ($x = 8; $x <= 22; $x++) {

    $sql = "SELECT DATEPART(hh,LastUpdateTime), SUM(TotalAmount) AS Total FROM Tickets WHERE DATEPART(hh,LastUpdateTime)=$x GROUP BY DATEPART(hh,LastUpdateTime)";
    $stmt = sqlsrv_query( $conn, $sql );
        if( $stmt === false) {
            die( print_r( sqlsrv_errors(), true) );
        }
        if( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
            $money = $row['Total'];
            echo "['".$x.":00', ".$money."],<br>";
        }
        else 
        {
                $money =0;
    echo "['".$x.":00', ".$money."],<br>";
        }
    //echo $x."<br>";
} 
sqlsrv_free_stmt( $stmt);
?>

答案 4 :(得分:0)

需要像if($money == 0)这样的条件,并在循环中将echo "['".$x.":00', ".$money."],<br>";写入for循环。

<?php
include 'go.php';
for ($x = 8; $x <= 22; $x++) {
    $money =0;    
    $sql = "SELECT DATEPART(hh,LastUpdateTime), SUM(TotalAmount) AS Total FROM Tickets WHERE DATEPART(hh,LastUpdateTime)=$x GROUP BY DATEPART(hh,LastUpdateTime)";
    $stmt = sqlsrv_query( $conn, $sql );
        if( $stmt === false) {
            die( print_r( sqlsrv_errors(), true) );
        }
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
            $money = $row['Total'];
            echo "['".$x.":00', ".$money."],<br>";
        }
        if($money == 0)
        echo "['".$x.":00', ".$money."],<br>";
} 
sqlsrv_free_stmt( $stmt);
?>