1

DB から 3 次元配列を作成し、それを JSON にエンコードするのに苦労しています。

私のDBには、timeline_table、content_table、pic_tableの3つのテーブルが含まれています。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 ループを使用して配列を埋めました。1 つのクエリの方が良い方法だと思いますが、間違っている場合は修正してください。この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 に関係なくすべての画像が 1 つの配列に含まれ、さらにコンテンツを追加すると、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 に答える