如何将多个表值组合成特定结构的数组?

时间:2017-02-15 05:58:39

标签: php mysql arrays

我在mysql中有这两个(两个)表,其中用户详细信息

1。用户

  user_id   | login         | email 
 ---------- | ------------- | ------------ 
  1         | PSS1          | email1@email.com
  2         | PSS2          | email2@email.com

2。细节

  id        | login         | datefrom     | dateto
 ---------- | ------------- | ------------   ------------
  1         | PSS1          | 2017-12-12   | 2017-12-13
  2         | PSS1          | 2017-12-15   | 2017-12-17
  3         | PSS2          | 2017-12-01   | 2017-12-02

我有这些PHP代码来编码JSON输出

第3。 getjson.php

<?php
    $connect = mysqli_connect("","","","");
    global $connect;

    if(isset($_POST['login'])) {
        $login = $_POST['login'];

        $sql = "SELECT * FROM users WHERE login='$login'";
        $result = mysqli_query($connect,$sql);
        if($result && mysqli_num_rows($result) > 0){
            while ($row = mysqli_fetch_array($result)) 
            {
                $login_db    = $row['login'];
                $email_db    = $row['email'];

                $sql2 = "SELECT * FROM details WHERE login='$login'";
                $result2 = mysqli_query($connect,$sql2);
                if($result2 && mysqli_num_rows($result2) > 0){
                    while ($row2 = mysqli_fetch_array($result2)) {

                        $dtfromdb = $row2['dtfrom'];
                        $dttodb   = $row2['dtto'];

                        $output = array(
                            'login'     => $login_db,     
                            'email'     => $email_db,
                            'movement'  => array(['dtfrom' => $dtfromdb,'dtto'  => $dttodb])
                        );
                        echo json_encode($output);
                    }
                }
                else {}
            }
        mysqli_free_result($result);
        }
        else { echo 'not found'; }
    }
?>

我的目标是实现类似下面的输出(4),当输入等于用户表中的PSS1 时,两个 dtfrom 的记录详情表中的dtto 显示在&#34;运动&#34;阵列

4。我的JSON输出目标

{
    "login":"PSS1",
    "email":"email1@email.com",
    "movement":
        [
            {
                "dtfrom":"2017-02-12",
                "dtto":"2017-02-13"
            },
            {

                "dtfrom":"2017-02-15",
                "dtto":"2017-02-17"
            }
        ]
}

问题是,我只能设法在详细信息表中显示单个值(5),如下所示

5。当前的JSON输出

{
    "login":"PSS1",
    "email":"email1@email.com",
    "movement":
        [
            {
                "dtfrom":"2017-02-15",
                "dtto":"2017-02-17"
            }
        ]
}

感谢有人可以帮我解决这个问题。感谢

2 个答案:

答案 0 :(得分:1)

你必须考虑数组的结构,有三个层次。因此,假设您只从用户表中获得一个结果:

$connect = mysqli_connect("","","","");
global $connect;

if(isset($_POST['login'])) {
    $login = $_POST['login'];

    $sql = "SELECT * FROM users WHERE login='$login'";
    $result = mysqli_query($connect,$sql);
    if($result && mysqli_num_rows($result) > 0){

        while ($row = mysqli_fetch_array($result)) 
        {
            $login_db    = $row['login'];
            $email_db    = $row['email'];

            $sql2 = "SELECT * FROM details WHERE login='$login'";
            $result2 = mysqli_query($connect,$sql2);

            $movements = array();

            if($result2 && mysqli_num_rows($result2) > 0){
                while ($row2 = mysqli_fetch_array($result2)) {

                    $dtfromdb = $row2['dtfrom'];
                    $dttodb   = $row2['dtto'];

                    $movements[] = array('dtfrom' => $dtfromdb, 'dtto' => $dttodb);

                }
            }
            else {}

            $output = array('login' => $login_db, 'email' => $email_db, 'movements' => $movements));

        }
        mysqli_free_result($result);
        echo json_encode($output);
    }
    else { echo 'not found'; }
}

答案 1 :(得分:0)

您应该在@tadman提到的SQL查询中创建所需的结构。此外,你应该采取建议,以避免@tadman提到的SQL注入 也就是说,为了使示例简单,请尝试以下方法:

<?php
    $connect = mysqli_connect("","","","");
    global $connect;

    if(isset($_POST['login'])) {
        $login = $_POST['login'];
        $rslt_ary = array();
        $sql = "SELECT users.login, user.email, details.datefrom, details.dateto
            FROM users 
            JOIN details
            ON details.login = users.login
            WHERE login='$login'
            ORDER BY users.login, details.datefrom;";
        $result = mysqli_query($connect,$sql);
        if($result && mysqli_num_rows($result) > 0){
            $curr_user = "";
            while ($row = mysqli_fetch_array($result)) 
            {
                if($curr_user != $row['login']) {
                    $rslt_ary = array('login' => $row['login'], 'email' => $row['email'], 'movement' => array());
                    $curr_user = $result['login'];
                }
                $rslt_ary['movement'] = array('dtfrom' => $row['datefrom'],'dtto' => $row['dateto']);
            }
        } else { 
            echo 'not found'; 
        }
        mysqli_free_result($result);
    }
    $json_result = json_encode($rslt);
?>