开发者

problem of while loop and array

$result=array();
$table_first = 'recipe';
$query = "SELECT * FROM $table_first";
$resouter = mysql_query($query, $conn);



while ($recipe = mysql_fetch_assoc($resouter, MYSQL_ASSOC)){
   $result['recipe']=$recipe;

$query2="SELECT ingredients.ingredient_id,ingredients.ingredient_name,ingredients.ammount FROM ingredients where rec_id = ".$recipe['rec_id'];
$result2 = mysql_query($query2, $conn);

 while($ingredient = mysql_fetch_assoc($result2)){


        $result['ingredient'] = $ingredient;

 }

 echo json_encode($result);
}

this code show me all the recipes but only the last ingredients i.e

{"recipe":{"rec_id":"14","name":"Spaghetti with Crab and Arugula","overview":"http:\/\/www","category":"","time":"2010-11-11 14:35:11","image":"localhost\/pics\/开发者_JS百科SpaghettiWithCrabAndArugula.jpg"},

"ingredient":{"ingredient_id":"55","ingredient_name":"test","ammount":"2 kg"}}{"recipe":{"rec_id":"15","name":"stew recipe ","overview":"http:\/\/www","category":"","time":"2010-11-11 14:42:09","image":"localhost\/pics\/stew2.jpg"},

"ingredient":{"ingredient_id":"25","ingredient_name":"3 parsnips cut into cubes","ammount":"11"}}

i want to output all the ingredient records relevant to recipe id 14 and this just print the last ingredient.


$result['ingredient'] = $ingredient;

Is replacing the variable $result['ingredient'] with the most recent $ingredient value each time, culminating with the last value returned, you should use:

$result['ingredient'][] = $ingredient;

To incrememnt/create a new value within the $result['ingredient'] array for each $ingredient. You can then output this array according to your needs. Using print_r($result['ingredient']) will show you its content...to see for yourself try:

while($ingredient = mysql_fetch_assoc($result2)){
        $result['ingredient'][] = $ingredient;


}

print_r($result['ingredient']);


If I understand correctly what you want to do, there is a way to optimize the code a lot, by fetching recipes and ingredients in one single query using a JOIN :

$recipes = array();
$recipe_ingredients = array();

$query = "SELECT * FROM recipe LEFT JOIN ingredients ON ingredients.rec_id=recipe.rec_id ORDER BY recipe.rec_id DESC";

$resouter = mysql_query($query, $conn);

$buffer_rec_id = 0;
$buffer_rec_name = "";
$buffer_rec_cat = "";

while( $recipe = mysql_fetch_array($resouter) )
{

  if( $buffer_rec_id != $result['recipe.rec_id'] )
  {
      $recipes[] = array( $buffer_rec_id, $buffer_rec_name, $buffer_rec_cat, $recipe_ingredients);

      $recipe_ingredients = array(  );

      $buffer_rec_id = $result['recipe.rec_id'];
      $buffer_rec_name = $result['recipe.rec_name'];
      $buffer_rec_cat = $result['recipe.rec_category'];
  }
  else
  {
      $recipe_ingredients[] = array( $result['ingredient_id'], $result['ingredient_name'], $result['ammount'] );

  }

}

$print_r($recipes);

This code should give you a multi-dimensional array that you can use later to get the data you want.

I let you adapt the code to have exactly the information you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜