doukong9316 2010-12-06 05:48
浏览 84
已采纳

如何提取数据json格式

i retrieved data from mysql tables in json using php i.e

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


$set=array();
while ($link = mysql_fetch_array($resouter, MYSQL_ASSOC)){
foreach ($link as $fieldname => $fieldvalue){
    $set[]= $fieldvalue;}
 $query2="SELECT ingredients.ingredient_id,ingredients.ingredient_name,ingredients.ammount FROM ingredients where rec_id = ".$link['rec_id'];
$result2 = mysql_query($query2, $conn);

 while ($rs = mysql_fetch_array($result2, MYSQL_ASSOC)){
     foreach($rs as $fieldname =>$fieldvalue){
         $set[]=$fieldvalue;
     }

 }

}
echo json_encode($set);

The result of the code is

["14","Spaghetti with Crab and Arugula","http:\/\/www","","2010-11-11 14:35:11","localhost\/pics\/SpaghettiWithCrabAndArugula.jpg",
"7","13 ounces spaghetti","10 kg",
"8","1 pound crabmeat","10"]

Note: The ingredients id starts after the image tag. 7 is the ingredient id followed by two fields "ingredients txt and amount" then 8 is another ingredient id relevant to the recipe id. like there is no ({) open or (}) close bracket in my result.

what i want to do is to output it in a correct json format. i.e

[
 {
  "rec_id": "14",
  "name":"Spaghetti with Crab and Arugula",
  "overview":"http:\/\/www",
  "category":"category",
                "time":"2010-11-11 14:35:11",
                "image":"localhost\/pics\/SpaghettiWithCrabAndArugula.jpg"
  "ingredients":
   {
    "ingredient":
     [                       {"ingredient_id":"7","ingredient_name":"13ounces spaghetti","amount":"10kg" },
{ "ingredient_id": "8", "ingredient_name": "1 pound crabmeat","amount":"10kg" },

     ]
   }]

and same for recipe id 15 and so on.......

so how can get this....!! any suggestions

  • 写回答

1条回答 默认 最新

  • doujumiao5024 2010-12-06 05:59
    关注

    You're outputting perfectly valid json.

    So look at how you're constructing $set ... see the problem yet?

    You're just pushing scalar values onto an array, so it should be no surprise that when you json-encode it you get a long array of scalars, with no structure. Your code is actively destroying the structure you want!

    I could fix your code for you, but I'm not going to. You need to look at your queries and loops, and figure out what's going on.

    You basically want to do something like this:

    $result = array();
    $recipes = mysql_query('....');
    while($recipe = mysql_fetch_assoc($recipes)){
        $result[$recipe['id']] = $recipe;
    
        $ingredients = mysql_query('...');
        while($ingredient = mysql_fetch_assoc($ingredients)){
            $result[$recipe['id']] = $ingredient;
        }
    }
    
    var_dump($result); //or echo json_encode($result);
    

    See the differences?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路