0

my PHP code look like this

test.php

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

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

        $sql   = "SELECT * FROM table 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'];
                $real_namedb = $row['real_name'];
                $email_db    = $row['email'];
                $dept_db     = $row['dept'];
                $division_db = $row['division'];

                $output= array('messages' => '1', 
                                                'login' => $login_db,
                                                'real_name' => $real_namedb,     
                                                'email' => $email_db, 
                                                'dept' => $dept_db, 
                                                'division' => $division_db
                               );
                echo json_encode($output);
                exit();
            }
        mysqli_free_result($result);
        }
        else {
            $output = array('messages' => '2', 'login' => 'wrong credentials from PHP code!');
            echo json_encode($output);
            echo mysqli_error($connect);
            exit();
        }
    }
    else
    {
        $output = array('messages' => '3', 'login' => 'No post data');
        echo json_encode($output);
        exit();
    }
?>
<!DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
    <form action="test.php" method="post">  
        <table>
            <tr>
                <td><i class="fa fa-unlock-alt"></i> </td>
                <td>Email : </td>
                <td><input type ="text" name="login" size="30"></td>
            </tr>
        </table>    

        <p><input type ="submit" name="Submit" value="DISPLAY"> </p>             
    </form>
</body>
</html>

My code above display JSON output like this

{"messages":"1","login":"ID0111","real_name":"NAME HERE","email":"[email protected]","dept":"IT","division":"MDO"}
{"messages":"1","login":"ID0112","real_name":"NAME HERE2","email2":"[email protected]","dept":"IT","division":"MDO"}

My question is how to modify PHP code above to make JSON output display like this ? What I mean is single login has multiple "messages" value

{
    "login":"ID0111",
    "real_name":"NAME HERE",
    "messages":
                [
                    {
                        "refno":"1234",
                        "email":"[email protected]",
                        "dept":"IT",
                        "division":"MDO"
                    },
                    {
                        "refno":"1345",
                        "email":"[email protected]",
                        "dept":"IT",
                        "division":"MDO"
                    },
                ]
}

Appreciate if someone can help. Thanks.

2
  • 1
    You exit within the loop. I don't see how you get 2 lines in output. Commented Feb 14, 2017 at 10:40
  • because he has encoded json two times Commented Feb 14, 2017 at 10:43

1 Answer 1

1

Create to two arrays,in first array give login,realname and in second array give refno,email,dept,division then merge the second with first array and then use json_encode().

  <?php

    $result =array();
    $message=array();

    $result['login'] ='ID0111';
    $result['real_name'] ='NAME HERE';
    $message['refno'] ='1234';
    $message['email'] ='[email protected]';
    $message['dept'] ='IT';
    $message['division'] ='MDO';
    $result['message'] =$message;

    echo json_encode($result);


  ?>
Sign up to request clarification or add additional context in comments.

1 Comment

How to combine both array into single output ? can you update the code in your answer ? thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.