2013-06-11 60 views
1
// Check connection 
if (mysqli_connect_errno($con)) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 


$result = mysqli_query($con,"SELECT * FROM recipe"); 


$encode = array(); 

while($allRow = mysqli_fetch_array($result)) 
    { 
     $new = array(
        'id' => $allRow['id']); 


     $encode[] = $new; 

    } 
echo json_encode($encode); 

mysqli_close($con); 
?> 

我有我的JSON格式錯誤..如何生成標題JSON格式

[{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"},{"id":"6"},{"id":"7"}] 

我需要有這樣的事情
enter link description here

+0

你只添加ID到數組,所以你當然不查看其他數據 – 2013-06-11 20:41:30

回答

2

好每一個你的食譜你會添加你想要顯示的屬性。

$new = array(
       'id' => $allRow['id'], 
       'title'=>$allRow['title'], 
       'ingredients'=>$allRows['ingredients']); 
    $encode[] = $new; 

然後你會創建一個包裝角落找尋當時的編碼對象調用json_encode

$json = json_encode(array("recipes"=>$encode));