2016-09-21 3 views
-1

다중 데이터 행을 표시하고 json 데이터를 표시해야합니다. 여기 내 코드입니다 :다중 데이터 행 표시 및 json 데이터 표시

<?php  
    $dinner_food_category=$_GET['DinnerFoodCategory']; 
    $conn = mysqli_connect("localhost","taig9_gen_user","GenAdmin1/Pass"); 
    if($conn) { 
     $select_database = mysqli_select_db($conn,"taig9_genumy"); 
     $select_query = "SELECT food_id,food_name,serving_type,serving_type_amount,singal_unit_weight FROM food_details WHERE category_id IN ('VEG00','VGB00','SUP00','CAK00')"; 
     $result = mysqli_query($conn, $select_query); 
     if (mysqli_num_rows($result) > 0) {       
      while($row = mysqli_fetch_assoc($result)) { 
       $foods_id=$row['food_id']; 
       $foods_name=$row['food_name']; 
       $foods_type=$row['serving_type']; 
       $foods_type_amount=$row['serving_type_amount']; 
       $singal_unit_weights=$row['singal_unit_weight']; 
      }   
      $data = array("FoodId" => $foods_id,"FoodNames" => $foods_name,"FoodType" => $foods_type,"FoodAmount" => $foods_type_amount,"SingalUnitWeight"=> $singal_unit_weights);   
     } 
     echo stripslashes(json_encode($data)); 
    }   
?> 
+0

'$ 데이터 [] = array'과에 넣어'while' –

+0

추가 질문 설명과 적절한 태그 – Shettyh

+0

을 언급 'while data '= array();'while'while'while''$ data [] = $ row;'그게 전부입니다. –

답변

0

당신이 food_ 변수를 덮어 while의 각 반복에, 그리고 while 루프가 끝난 후 - 당신은 마지막 행에 대해서만 값을 가지고있다. 당신이 [] 표기와 while 루프 내에서 $data에 변수를 추가해야이 문제를 방지하려면 :

while ($row = mysqli_fetch_assoc($result)) { 
    $data[] = array(
     "FoodId" => $row['food_id']; 
     "FoodNames" => $row['food_name']; 
     "FoodType" => $row['serving_type']; 
     "FoodAmount" => $row['serving_type_amount']; 
     "SingalUnitWeight" => $row['singal_unit_weight']; 
    ); 
}   
// using `stripslashes` is useless 
echo json_encode($data); 
0
if (mysqli_num_rows($result) > 0) { 
    $data = []; 
    foreach ($result as $row) { 
     $foods_id=$row['food_id']; 
     $foods_name=$row['food_name']; 
     $foods_type=$row['serving_type']; 
     $foods_type_amount=$row['serving_type_amount']; 
     $singal_unit_weights=$row['singal_unit_weight']; 
     $data[] = ["FoodId" => $foods_id,"FoodNames" => $foods_name,"FoodType" => $foods_type,"FoodAmount" => $foods_type_amount,"SingalUnitWeight"=> $singal_unit_weights];   
    } 
    echo stripslashes(json_encode($data)); 
}