简体   繁体   English

使用 PHP 和 MySQLi 对 WHERE 子句日期前后的数据进行排序

[英]Using PHP and MySQLi to sort data with WHERE clause date before and after

I'm a little new to PHP / MySQLi so think doing ok so far, however I am a little stuck.我对 PHP / MySQLi 有点陌生,所以认为到目前为止还可以,但是我有点卡住了。

I want to show the total SUM for OPEN invoices but separate them to show due invoices and overdue / late invoices.我想显示未结发票的总金额,但将它们分开以显示到期发票和逾期/逾期发票。

Here are my functions:这是我的功能:

function getLateInvoicesTotal() {
    // Connect to the database
    $mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME);

    // output any connection error
    if ($mysqli->connect_error) {
        die('Error : ('.$mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    $query = "SELECT SUM(`total`) as `total` FROM invoices WHERE invoice_due_date < NOW() AND status = 'open'";

    // mysqli select query
    $results = $mysqli->query($query);

    while($row = $results->fetch_assoc()) {
        $value = $row["total"];
        $value = $value / 100;
        setlocale(LC_MONETARY, 'en_GB.UTF-8');
        $value = money_format("%.2n", $value);
    print $value;
    }  

  // Frees the memory associated with a result
    $results->free();

    // close connection 
    $mysqli->close();
}

function getOpenInvoicesTotal() {
    // Connect to the database
    $mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME);

    // output any connection error
    if ($mysqli->connect_error) {
        die('Error : ('.$mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    $query = "SELECT SUM(`total`) as `total` FROM invoices WHERE invoice_due_date < NOW() AND status = 'open'";

    // mysqli select query
    $results = $mysqli->query($query);

    while($row = $results->fetch_assoc()) {
        $value = $row["total"];
        $value = $value / 100;
        setlocale(LC_MONETARY, 'en_GB.UTF-8');
        $value = money_format("%.2n", $value);
    print $value;
    }  

  // Frees the memory associated with a result
    $results->free();

    // close connection 
    $mysqli->close();
}

In my table I have 2 columns for dates, invoice_due and invoice_due_date which for example has values 16/01/2017 16/06/2017在我的表中,我有 2 列日期,invoice_due 和 invoice_due_date,例如其值为 16/01/2017 16/06/2017

Does anyone know how I can modify my 2 functions to collect the data I want for due and overdue invoices?有谁知道我如何修改我的 2 个函数来收集我想要的到期和过期发票的数据? It is working fine showing the amounts I am just stuck with defining them apart.它工作正常,显示我只是坚持将它们分开定义的数量。

You are using invoice_due_date in both the query.您在两个查询中都使用了invoice_due_date

If you want invoice_due in one function change the where query from invoice_due_date to invoice_due .如果你想invoice_due在一个功能变更,其中来自查询invoice_due_dateinvoice_due

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM