客户明智的总销售额,每月总销售额和年度总计

时间:2014-07-30 19:34:27

标签: php mysql

我正在尝试检索数据客户端明智的总销售额/月, 每月总销售额和年度变量的总计 一年来自ajax

<?php
//database connection

if(!empty($_GET['year'])) {
    $year = $_GET['year'];
}


if($year != '') {
    ?>
    <table id="dataTables-example" class="table table-striped table-bordered table-hover dataTable no-footer"
           aria-describedby="dataTables-example_info">
        <tr>
            <th>Month</th>
            <th>Client</th>
            <th>Total Sales</th>
        </tr>
    <?php
    $sql1 = "SELECT MONTH(order_date) as mont1 FROM ri_order 
              WHERE YEAR(order_date) = '".$year."' group by client, MONTH(order_date)
     order by MONTH(order_date) desc ";
    $result1 = mysql_query($sql1) or die(mysql_error());

    $grand_total = 0;
    while ($row1 = mysql_fetch_array($result1)) {
        $sql = "SELECT *, SUM(order_amount) as client_order_amount, MONTH(order_date) as mont FROM ri_order WHERE YEAR(order_date) =
     '".$year."' and MONTH(order_date) = '".$row1['mont1']."' group by
     client, MONTH(order_date) order by MONTH(order_date) desc ";
        $result = mysql_query($sql) or die(mysql_error());
        if($result) {
            $month_total = 0;
            while ($row = mysql_fetch_array($result)) {
                $monthName = date('F', mktime(0, 0, 0, $row['mont'], 10));
                $client = "SELECT * FROM ri_client WHERE id = '".$row['client']."'";
                $resultCli = mysql_query($client) or die(mysql_error());

                if($resultCli) {
                    while ($rowcli = mysql_fetch_array($resultCli)) {
                        ?>
                            <tr>
                                <td align="left"><?= $monthName; ?></td>
                                <td align="left"><?= $rowcli['client']; ?></td>
                                <td align="left">$<?= number_format($row['client_order_amount'], 2); ?></td>
                            </tr>
                            <?php
                        }
                        $month_total += $row['client_order_amount'];
                    }
                }
                ?>
                <td align="left">&nbsp;</td>
                <td align="left"><strong>Total:</strong></td>
                <td align="left"><strong>
                        $<?= number_format($month_total, 2); ?>
                    </strong></td>
            </tr>
            <tr>
                <td colspan="3">&nbsp;</td>
            </tr>
            <?php
        }
        $grand_total+=$month_total;
    }
    ?>
    <tr>
        <td colspan="3">&nbsp;</td>
    </tr>
    <td align="left">&nbsp;</td>
    <td align="left"><strong>Grand Total:</strong></td>
    <td align="left"><strong>
            $<?= number_format($grand_total, 2); ?>
        </strong></td>
    </tr>

    </table>
    <?php
} else {
    echo "<font color='#FF0000'><strong>No Orders under this year!</strong></font>";
}

enter image description here

0 个答案:

没有答案