2017-02-14 31 views
0

我的PHP代碼看起來像這樣如何在PHP mysqli中編碼JSON輸出?

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> 

我上面這樣

{"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"} 

我的問題顯示JSON輸出代碼是如何修改上面的PHP代碼,以使JSON輸出顯示是這樣的嗎?我的意思是單點登錄有多個「消息」值

{ 
    "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" 
        }, 
       ] 
} 

感激,如果有人可以提供幫助。謝謝。

+1

你'exit'內環路。我不明白你如何獲得2行輸出。 –

+0

因爲他已經編碼的JSON兩次 –

回答

1

創建到兩個陣列,在第一陣列給登錄,實名和在第二陣列給引用號,電子郵件,DEPT,除法然後合併所述第二與第一陣列,然後使用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); 


    ?> 
+0

如何既陣列爲單輸出結合起來?你能更新答案中的代碼嗎?謝謝 – Jamilah

+0

雅我會更新u代碼給一些時間。 – user123

+0

在這裏我給了示例代碼,一旦它可以幫助檢查它。 – user123