MySQLI查询分组依据

时间:2018-07-13 16:55:27

标签: php mysql sql mysqli

我有一个Android应用程序,可以显示用户的在线/离线历史记录。当发生任何活动时,我正在保存用户的联机/脱机时间戳。我正在尝试实现可以​​比较两个用户的在线/离线历史记录的功能。要在android中提供数据,我的API如下所示

compareNumber.php

<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$response = array("error" => FALSE);

if (isset($_POST['firstNumber']) && isset($_POST['secondNumber']) && isset($_POST['email']) && isset($_POST['date'])) {
    $firstNumber = $_POST["firstNumber"];
    $secondNumber = $_POST["secondNumber"];

    $email = $_POST["email"];
    $date = $_POST["date"];

    $response = $db->compareNumber($firstNumber, $secondNumber, $email, $date);

    echo json_encode($response);
} else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameters email, date and firstNumber and secondNumber are missing!";
    echo json_encode($response);
}
?>

我的功能看起来像这样

public function compareNumber($firstNumber, $secondNumber, $email, $date) {
    $response = array('code' => 0, 'error' => false);
    $endDate = date('Y-m-d', strtotime("+1 day", strtotime($date))) . " " . explode(" ", $date)[1];

    $stmt = $this->conn->prepare("SELECT id FROM user WHERE email = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows) {
        $user =  $result->fetch_assoc();            
        $user_id = $user['id'];
        $stmt->close();

        $response["received"]   = $date;
        $response["created_at"] = $date;
        $response["end"]        = $endDate;
        $response["code"]       = 1;

        $stmt = $this->conn->prepare("SELECT number, number_status, status_time FROM number_status WHERE (number = ? OR number = ?) AND status_time > ? AND status_time < ? ORDER BY status_time DESC");
        $stmt->bind_param("ssss", $firstNumber, $secondNumber, $date, $endDate);
        $stmt->execute();
        $result = $stmt->get_result();

        $number = array();
        $startTime = array();
        $endTime = array();

        if ($result->num_rows) {
            $user =  $result->fetch_assoc();            
            $user_id = $user['id'];

            $first = true;
            while($row = $result->fetch_row()) {
            //to skip offline if first entry is online
                if($first) {
                    $first = false;
                    if($row[1] == 1) {
                        continue;
                    }
                }

                array_push($number, $row[0]);

                if($row[1] == 1) {
                    array_push($startTime, $row[2]);
                }
                else {
                    array_push($endTime, $row[2]);
                }
            }
        }

        $stmt->close();

        $response["number"] = $number;
        $response["startTime"] = $startTime;
        $response["endTime"] = $endTime;
    }

    return $response;
}

但是与此同时,我正在弄混数字输入。我想要正确的离线和在线分组输入,可以提供如下图所示的结果。让我知道是否有人可以给我这个主意。

Example Image

谢谢

0 个答案:

没有答案