如何读取表并在PHP中将它们呈现为JSON?

时间:2014-12-24 11:55:44

标签: php json slim

我有类别和子类别结构的数据库:

category
Image of category table

subcategory
Image of subcategory table

我希望通过PHP以所需的格式使用JSON。我已经尝试了大部分时间但是没有用。

{
"success":"true",
"category_1":{
"cat_id":"1",
"cat_name":"menu1",
"cat_img":"cat1.jpg",
"sub_category":[
{
"sub_id":"1",
"sub_name":"sub name"
},
{
"sub_id1":"2",
"sub_name1":"sub name"
},
{
"sub_id2":"3",
"sub_name2":"sub name"
},
{
"sub_id3":"4",
"sub_name3":"sub name"
}
]
},
"category_2":{
"cat_id":"2",
"cat_name":"menu2",
"cat_img":"cat2.jpg",
"sub_category":[
{
"sub_id":"1",
"sub_name":"sub name"
},
{
"sub_id1":"2",
"sub_name1":"sub name"
}
]
},
"category_3":{
"cat_id":"3",
"cat_name":"menu3",
"cat_img":"cat3.jpg",
"sub_category":[
{
"sub_id":"1",
"sub_name":"sub name"
},
{
"sub_id1":"2",
"sub_name1":"sub name"
},
{
"sub_id3":"",
"sub_name1":"sub name"
}
]
}
}

我已尝试过此代码,但它无效。我更喜欢PDO解决方案。

$servername = "localhost";
$username = "res_user";
$password = "Res@123";
$dbname = "res_db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

 $sql = "SELECT *, category.id AS catId FROM category INNER JOIN sub_category ON category.id = sub_category.category_id ORDER BY category.id, sub_category.category_id";
$res = mysqli_query($conn, $sql);
$categoryArray = array();
$oldCatId = 0;
while ($row = mysqli_fetch_assoc($res)) {
    if ($row['catId'] != $oldCatId) {
        $categoryArray[$row['catId']] = array(
            'success' => true,
            'category_' . $row['catId'] = array(
        'cat_id' => $row["catId"],
        'cat_name' => $row['cat_name'],
        'cat_img' => $row['cat_img'],
        'sub_category' => array(
            'sub_id' => $row['category_id'],
            'sub_name' => $row['sub_name']
        )
            )
        );
     $oldCatId = $row['catId'];
    } else {
        $categoryArray[$row['catId']]['category_' . $row['catId']]['sub_category'][] = array(
            'sub_id' => $row['category_id'],
            'sub_name' => $row['sub_name']
        );
    }
}


  echo  json_encode($categoryArray);

1 个答案:

答案 0 :(得分:0)

你需要这样的东西:从类别id排序的类别中取出所有行,并用子类别加入它。

然后你在你的resutls上循环。您检查,当前类别ID是否等于之前的cat id。如果没有,那么构建主数据数组,并添加第一个子类别,如果没有,则只需添加下一个子类别。

$sql = "
    SELECT *, category.id AS catId
    FROM category
    INNER JOIN subcategory ON category.id = subcategory.category_id
    ORDER BY category.id, subcategory.category_id
";
$res = mysqli_query($conn, $sql);
$categoryArray = array();
$oldCatId = 0;
while ($row = mysqli_fetch_assoc($res)) {
    if ($row['catId'] != $oldCatId) {
        $categoryArray[$row['catId']] = array(
            'success' => true,
            'category_' . $row['catId'] = array(
        'cat_id' => $row["catId"],
        'cat_name' => $row['cat_name'],
        'cat_img' => $row['cat_img'],
        'sub_category' => array(
            'sub_id' => $row['category_id'],
            'sub_name' => $row['sub_name']
        )
            )
        );
        $oldCatId = $row['catId'];
    } else {
        $categoryArray[$row['catId']]['category_' . $row['catId']]['sub_category'][] = array(
            'sub_id' => $row['category_id'],
            'sub_name' => $row['sub_name']
        );
    }
}