【问题标题】:Json from two mysql tables in PHP require output in hierarchyPHP中两个mysql表中的Json需要层次结构中的输出
【发布时间】:2017-05-09 02:19:45
【问题描述】:

我有两张桌子,

1 - 类别

2 - 子类别

 Category (table 1)
---------------
Cid    |   Name

1      |   Vegetable

2      |   Fruit


SubCategory  (table 2)
----------------
Cid  |  Sid | S_name

 1   |   1  | Carrot

 1   |   2  | Beans

 2   |  3   | Mango

我想要 Json 中的结果:-

  -> Category found

       ->  Vegetable

            -> carrot

             -> beans

       ->  Fruit

             -> mango

我正在使用下面的代码!任何人都可以帮助我更正我的代码并获得我上面提到的输出!我试过很多次。但没有得到解决方案!请帮助我获得目录形式的输出。谢谢

`$srtResult = "SELECT * FROM `category` `c` LEFT JOIN `subcategory` `s` ON 
(`c`.`cid` = `s`.`cid`)";

 //Execute Qu**strong text**ery
    $result=mysql_query($srtResult);
   //Iterate Throught The Results

  while ($row = mysql_fetch_assoc($result, MYSQL_ASSOC)) 
{ $count = $row['cid'];
 while($row['cid'] = $count)
 { $subcatitem[] = Array( "cid" => $row['cid'], "sid" =>$row['sid'],          
"sname" => $row['sc_name'] );
  }
  $json['category'][] = Array("category" => Array( "cid" => $row['cid'], 
"name" => $row['name'], "subcategory" => Array( $subcatitem))); 
  } 

  header('Content-type: application/json');
echo json_encode($json);`   

【问题讨论】:

    标签: php mysql arrays json


    【解决方案1】:

    这还不够吗?

    $srtResult = "SELECT * FROM `category` `c` LEFT JOIN `subcategory` `s` ON 
    (`c`.`cid` = `s`.`cid`)";
    $result = mysql_query($srtResult);
    
    while ($row = mysql_fetch_assoc($result, MYSQL_ASSOC)) {
      $json['category'][$row['name']] = $row['sc_name']; 
    } 
    
    header('Content-type: application/json');
    echo json_encode($json);
    

    编辑:

    在 cmets 中进行更多解释后,所需的 PHP 数组结构(给出预期的 JSON 字符串)应如下所示:

    Array
    (
        [success] => 1
        [message] => Category found in Database
        [Category] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [name] => vegetable
                        [Subcategory] => Array
                            (
                                [0] => Array
                                    (
                                        [cid] => 1
                                        [sid] => 1
                                        [sc_name] => carrot
                                    )
    
                                [1] => Array
                                    (
                                        [cid] => 1
                                        [sid] => 2
                                        [sc_name] => beans
                                    )
    
                            )
    
                    )
    
                [1] => Array
                    (
                        [id] => 2
                        [name] => Fruit
                        [Subcategory] => Array
                            (
                                [0] => Array
                                    (
                                        [cid] => 1
                                        [sid] => 1
                                        [sc_name] => Mango
                                    )
    
                            )
    
                    )
    
            )
    
    )
    

    【讨论】:

    • Picard,我试过这个,但它告诉我;- mysql_fetch_assoc() 期望参数 1 是资源,布尔值在第 10 行给出。
    • 对于 SELECT,(...) mysql_query() 成功时返回资源,错误时返回 FALSE。 您的查询中一定有错误 - $srtResult。也许表名不应该不是subcategory而是SubCategory?您可以通过以下方式检查错误:if (!$result) { die('Invalid query: ' . mysql_error()); }
    • 谢谢 Picard,我想要 Json 中的输出,请指导我编写代码! {"success":1,"message":"在数据库中找到的类别","Category": [{"id":"1","name":"vegetable","Subcategory": [{"cid": "1", "sid":"1","sc_name":"carrot"}, {"cid":"1", "sid":"2","sc_name":"beans"}]}, { "id":"2","name":"Fruit","Subcategory": [{"cid":"1", "sid":"1","sc_name":"Mango"}]}]}
    • 我已经更新了我的答案,以显示如果你想要上面的 JSON,你需要构建的 PHP 数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 2022-01-26
    • 2011-05-26
    • 1970-01-01
    相关资源
    最近更新 更多