alter mysql result for json to index by key

jchrr9hc  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(280)

我有一个mysql结果,我把它作为一个数组获取并编码为json,如下所示:

$getDisplayPage = "
SELECT p.id as pageID, page_type_id, display_id, slide_order, duration, background_img, pn.ID as panel_id, panel_type_id, cont_id, c.ID as contID, content 
FROM pages p
inner join panels pn
on p.id = pn.page_id 
inner join content c
on pn.cont_id = c.id
WHERE p.active = 1
and pn.active = 1
AND p.display_id = '".$display."'
";

$showDisplayResult = $mysqlConn->query($getDisplayPage);
while($row=mysqli_fetch_assoc($showDisplayResult))
    {
        $rows[] = $row;
    }
$showDisplays = json_encode($rows);

它为每个条目返回一行,但我需要以某种方式将其更改为按页面id索引。如果您看到下面的json,它将返回正确的数据,但实际上它应该只有3个对象/行,而不是5个。我需要能够访问给定pageid的所有属性。如何修改它以获得正确的json对象?

[{"pageID":"93",
    "page_type_id":"2",
    "display_id":"2",
    "slide_order":null,
    "duration":"74",
    "background_img":"images\/bg_rainbow.svg",
    "panel_id":"86",
    "panel_type_id":"2",
    "cont_id":"138",
    "contID":"138",
    "content":"\r\n\r\n\r\n<\/head>\r\n\r\nLeft 93<\/p>\r\n<\/body>\r\n<\/html>"},
{"pageID":"93",
    "page_type_id":"2",
    "display_id":"2",
    "slide_order":null,
    "duration":"74",
    "background_img":"images\/bg_rainbow.svg",
    "panel_id":"87",
    "panel_type_id":"3",
    "cont_id":"139",
    "contID":"139",
    "content":"\r\n\r\n\r\n<\/head>\r\n\r\nRight 93<\/p>\r\n<\/body>\r\n<\/html>"},
{"pageID":"95",
    "page_type_id":"1",
    "display_id":"2",
    "slide_order":null,
    "duration":"123",
    "background_img":"images\/bg_rainbow.svg",
    "panel_id":"90",
    "panel_type_id":"1",
    "cont_id":"142",
    "contID":"142",
    "content":"\r\n\r\n\r\n<\/head>\r\n\r\nTesting a full page for ID 95<\/p>\r\n<\/body>\r\n<\/html>"},
{"pageID":"105",
    "page_type_id":"2",
    "display_id":"2",
    "slide_order":null,
    "duration":"54",
    "background_img":"images\/bg_rainbow.svg",
    "panel_id":"97",
    "panel_type_id":"2",
    "cont_id":"149",
    "contID":"149",
    "content":"\r\n\r\n\r\n<\/head>\r\n\r\nThis is left content<\/p>\r\n<\/body>\r\n<\/html>"},
{"pageID":"105",
    "page_type_id":"2",
    "display_id":"2",
    "slide_order":null,
    "duration":"54",
    "background_img":"images\/bg_rainbow.svg",
    "panel_id":"98",
    "panel_type_id":"3",
    "cont_id":"150",
    "contID":"150",
    "content":"\r\n\r\n\r\n<\/head>\r\n\r\nThis is right content<\/p>\r\n<\/body>\r\n<\/html>"}]
llycmphe

llycmphe1#

$getDisplayPage = "
SELECT p.id as pageID, page_type_id, display_id, slide_order, duration, background_img, pn.ID as panel_id, panel_type_id, cont_id, c.ID as contID, content 
FROM pages p
inner join panels pn
on p.id = pn.page_id 
inner join content c
on pn.cont_id = c.id
WHERE p.active = 1
and pn.active = 1
AND p.display_id = '".$display."'
";

$showDisplayResult = $mysqlConn->query($getDisplayPage);
while($row=mysqli_fetch_assoc($showDisplayResult))
    {
        $rows[$row['pageID']][] = $row;
    }
$showDisplays = json_encode($rows);

相关问题