2016-09-21 11 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'と –

+0

は質問の説明を追加し、適切なタグ – Shettyh

+0

を言及while''でそれを置きます'$ data [] = $ row;'それはそれです。 –

答えて

0

whileの各反復では、あなたがfood_変数が上書きされ、そして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)); 
} 
関連する問題