1

我正在努力尝试从我的数据库创建一个三维数组,并将其编码为 JSON。

我的数据库包含 3 个表,timeline_table、content_table 和 pic_table。我的 JSON 需要以下结构:

{"timeline:"{"content":{"pictures:"{}}}}

这是我当前的 PHP 代码:

$get = 1;

$results = mysql_query("

SELECT timeline_table.*, content_table.*, pic_table.*
FROM timeline_table
    JOIN content_table
        ON content_table.tl_ID = timeline_table.tl_ID
    JOIN pic_table
        ON pic_table.content_ID = content_table.content_ID
WHERE timeline_table.tl_ID = $get   

") or die(mysql_error());

while($row = mysql_fetch_assoc($results)){

    $timeline['timeline'][] = array(
        'tl_ID' => $row['tl_ID'],
        'tl_name' => $row['tl_name'],
        'tl_date' => $row['tl_date'],
        'tl_desc' => $row['tl_desc'],
    );

    $timeline['timeline']['content'][] = array(
        'content_ID' => $row['content_ID'],
        'tl_ID' => $row['tl_ID'],
        'content_time' => $row['content_time'],
        'content_date' => $row['content_date'],
        'content_title' => $row['content_title'],
        'content_content' => $row['content_content'],
        'content_category' => $row['content_category'],
        'content_mapLat' => $row['content_mapLat'],
        'content_mapLng' => $row['content_mapLng'],
        'content_zoomLvl' => $row['content_zoomLvl'],       
    );

    $timeline['timeline']['content']['pictures'][] = array(
        'pic_ID' => $row['pic_ID'],
        'content_ID' => $row['content_ID'],
        'pic_path' => $row['pic_path'],
        'pic_desc' => $row['pic_desc'],
        'pic_link' => $row['pic_link']
    );

}
echo stripslashes(json_encode($timeline));
}

我还尝试对每个表使用 1 个查询,并使用 3 个 while 循环来填充数组。我相信一个查询是更好的方法,但如果我错了,请纠正我。这个 php 给了我以下 JSON:

{
   "timeline":{
      "0":{
         "tl_ID":"1",
         "tl_name":"Tidslinje 1",
         "tl_date":"2013-01-16",
         "tl_desc":"Test av tl_table"
      },
      "content":{
         "0":{
            "content_ID":"1",
            "tl_ID":"1",
            "content_time":"16:00:00",
            "content_date":"2013-01-17",
            "content_title":"Test",
            "content_content":"Test content number one.",
            "content_category":"test",
            "content_mapLat":null,
            "content_mapLng":null,
            "content_zoomLvl":null
         },
         "pictures":[
            {
               "pic_ID":"1",
               "content_ID":"1",
               "pic_path":"http://i.imgur.com/F6RmDFt.jpg",
               "pic_desc":"katt",
               "pic_link":"http://i.imgur.com/F6RmDFt.jpg"
            },
            {
               "pic_ID":"3",
               "content_ID":"3",
               "pic_path":"http://i.imgur.com/POum7eK.jpg",
               "pic_desc":"seamonster",
               "pic_link":"http://i.imgur.com/POum7eK.jpg"
            }
         ]
        }
    }
}

无论 content_ID 如何,所有图片都在一个数组中,如果我添加更多内容,则 ID 为 2,3 等的内容将位于图片数组下。我想要它们所属的 content_ID 下的数组中的图片,以及它们所属的时间轴下的内容。我还希望数组键是“时间轴”、“内容”和“图片”,而不是整数。

希望这是可以理解的,非常感谢任何帮助!

编辑:解决了!

$get = 1;

$result = mysql_query("

SELECT t.tl_ID, t.tl_name, t.tl_date, t.tl_desc, c.content_ID, c.content_time, c.content_date, c.content_title, c.content_content, c.content_category, p.pic_ID, p.pic_path, p.pic_desc, p.pic_link
FROM timeline_table t
    LEFT JOIN content_table c ON t.tl_ID = c.tl_ID
    LEFT JOIN pic_table p ON c.content_ID = p.content_ID
WHERE t.tl_ID = $get
ORDER BY t.tl_ID, c.tl_ID, p.content_ID

") or die(mysql_error()); 

$jsonData = array();

$tl_ID = 0;
$content_ID = 0;

$timelineIndex = -1;
$contentIndex = -1;

while($row = mysql_fetch_assoc($result)){

    if($tl_ID != $row['tl_ID']){
        $timelineIndex++;
        $contentIndex = -1;
        $tl_ID = $row['tl_ID'];

        $jsonData[$timelineIndex]['tl_ID'] = $row['tl_ID'];
        $jsonData[$timelineIndex]['tl_name'] = $row['tl_name'];
        $jsonData[$timelineIndex]['tl_date'] = $row['tl_date'];
        $jsonData[$timelineIndex]['tl_desc'] = $row['tl_desc'];

        $jsonData[$timelineIndex]['content'] = array();
    }

    if($content_ID != $row['content_ID']){
        $contentIndex++;
        $content_ID = $row['content_ID'];

        $jsonData[$timelineIndex]['content'][$contentIndex]['content_ID'] = $row['content_ID'];
        $jsonData[$timelineIndex]['content'][$contentIndex]['content_time'] = $row['content_time'];
        $jsonData[$timelineIndex]['content'][$contentIndex]['content_date'] = $row['content_date'];
        $jsonData[$timelineIndex]['content'][$contentIndex]['content_title'] = $row['content_title'];
        $jsonData[$timelineIndex]['content'][$contentIndex]['content_content'] = $row['content_content'];
        $jsonData[$timelineIndex]['content'][$contentIndex]['content_category'] = $row['content_category'];


        $jsonData[$timelineIndex]['content'][$contentIndex]['pictures'] = array();
    }

    $jsonData[$timelineIndex]['content'][$contentIndex]['pictures'][] = array(
        'pic_ID' => $row['pic_ID'],
        'pic_path' => $row['pic_path'],
        'pic_desc' => $row['pic_desc'],
        'pic_link' => $row['pic_link']
    );

}
echo stripslashes(json_encode($jsonData));
}
4

1 回答 1

0

编辑,试试这个:

$timeline['timeline'][] = array(
        'tl_ID' => $row['tl_ID'],
        'tl_name' => $row['tl_name'],
        'tl_date' => $row['tl_date'],
        'tl_desc' => $row['tl_desc'],
        'content' => array(
          'content_ID' => $row['content_ID'],
          'tl_ID' => $row['tl_ID'],
          'content_time' => $row['content_time'],
          'content_date' => $row['content_date'],
          'content_title' => $row['content_title'],
          'content_content' => $row['content_content'],
          'content_category' => $row['content_category'],
          'content_mapLat' => $row['content_mapLat'],
          'content_mapLng' => $row['content_mapLng'],
          'content_zoomLvl' => $row['content_zoomLvl'],
          'pictures' => array(
            'pic_ID' => $row['pic_ID'],
            'content_ID' => $row['content_ID'],
            'pic_path' => $row['pic_path'],
            'pic_desc' => $row['pic_desc'],
            'pic_link' => $row['pic_link']
          );      
        );
);
于 2013-02-19T12:46:46.637 回答