2013-01-03 45 views
0

我在輸出json之前合併數據時遇到問題。目標是讓json中的每個對象都包含每個'post'的所有數據。Json的合併行編碼

下面的代碼:

$results = mysql_query("SELECT dfuw_posts.ID, dfuw_posts.post_title, 
dfuw_postmeta.meta_key, dfuw_postmeta.post_id, dfuw_postmeta.meta_value FROM 
dfuw_posts INNER JOIN dfuw_postmeta ON dfuw_posts.ID = dfuw_postmeta.post_id WHERE 
dfuw_posts.post_type = 'poi' AND dfuw_posts.post_status = 'publish' AND 
dfuw_postmeta.meta_key IN ('_poi_loc' ,'_poi_level' ,'_poi_type')"); 

$pois = array(); 
while($r = mysql_fetch_assoc($results)){ 
    $pois[] = $r; 
} 
echo json_encode($pois); 

輸出:

[ 
{ 
    "ID": "320", 
    "post_title": "Bank", 
    "meta_key": "_poi_loc", 
    "post_id": "320", 
    "meta_value": "70.4845662217412|-84.5123291015625" 
}, 
{ 
    "ID": "320", 
    "post_title": "Bank", 
    "meta_key": "_poi_level", 
    "post_id": "320", 
    "meta_value": "easy" 
}, 
{ 
    "ID": "320", 
    "post_title": "Bank", 
    "meta_key": "_poi_type", 
    "post_id": "320", 
    "meta_value": "bank" 
}, 
{ 
    "ID": "324", 
    "post_title": "A Creature", 
    "meta_key": "_poi_type", 
    "post_id": "324", 
    "meta_value": "mob" 
}, 
{ 
    "ID": "324", 
    "post_title": "A Creature", 
    "meta_key": "_poi_level", 
    "post_id": "324", 
    "meta_value": "med" 
}, 
{ 
    "ID": "324", 
    "post_title": "A Creature", 
    "meta_key": "_poi_loc", 
    "post_id": "324", 
    "meta_value": "70.4845662217412|-84.5123291015625" 
} 
] 

我想對於每一個職位進行捆綁,如:

{ 
    "title": "A Creature", 
    "location": "70.4845662217412|-84.5123291015625", 
    "type": "mob", 
    "level": "hard" 
} 

這是可以實現的?如果是這樣,任何建議送我​​在正確的道路將不勝感激!

在此先感謝!

+0

檢查了這個 - http://stackoverflow.com/questions/13156159/json-menu-structure-from-mysql/13157562#13157562 – Joddy

+0

@Joddy,謝謝!將研究這一點。這看起來像我想要實現的。一旦我離開並睡一會兒,我會試試。 – nmynarcik

回答

0

下面是我最終解決我的問題有。

$results = mysql_query(" 
    SELECT 
     dfuw_posts.ID AS pid, 
     dfuw_posts.post_title AS title, 
     dfuw_postmeta.meta_key AS mkey, 
     dfuw_postmeta.post_id AS pmid, 
     dfuw_postmeta.meta_value AS mval 
    FROM 
     dfuw_posts 
    INNER JOIN 
     dfuw_postmeta ON dfuw_posts.ID = dfuw_postmeta.post_id 
    WHERE 
     dfuw_posts.post_type = 'poi' 
    AND 
     dfuw_posts.post_status = 'publish' 
    AND 
     dfuw_postmeta.meta_key IN ('_poi_loc' ,'_poi_level' ,'_poi_type')"); 

    $jsary = ["pois" => []]; 
    $lastPid = 0; 
    $currentPid = 0; 
    $title = ""; 
    $ifff = 0; 
    $elss = 0; 
    while($row = mysql_fetch_array($results)) 
    { 
     $currentPid = $row['pid']; 
     $title = $row['title']; 

     $cmd = $row['mkey']; 
     $cmt = $row['mval']; 

     if($lastPid != $currentPid) 
     { 
       $insAry = []; 
       $insAry = ["title"=> $title, $cmd => $cmt]; 
       array_push($jsary["pois"], $insAry); 
       $lastPid = $currentPid; 
       $ifff = $ifff + 1; 
       $currentPid = 0; 
     } 
     else 
     { 
       $ind = 0; 
       if($ifff > 0) 
       { 
         $ind = $ifff-1; 
       } 
       $insAry = [$cmd => $cmt]; 
       $jsary["pois"][$ind][$cmd] = $cmt; 
     } 
     } 

echo json_encode($jsary); 

此輸出該數據作爲精美:

{ 
"pois": [ 
{ 
    "title": "Bank", 
    "_poi_loc": "70.4845662217412|-84.5123291015625", 
    "_poi_level": "easy", 
    "_poi_type": "bank" 
}, 
{ 
    "title": "A Creature", 
    "_poi_type": "mob", 
    "_poi_level": "med", 
    "_poi_loc": "70.4845662217412|-84.5123291015625" 
} 
] 

感謝所有的答覆。 @Joddy - 你的帖子幫助我得到了最終的結果。 TY!

0

在mysql查詢中提供自定義標題將解決您的問題。

$results = mysql_query(" 
     SELECT 
      dfuw_posts.ID, 
      dfuw_posts.post_title AS title, 
      dfuw_postmeta.meta_key, 
      dfuw_postmeta.post_id, 
      dfuw_postmeta.meta_value AS location 
     FROM 
      dfuw_posts 
     INNER JOIN 
      dfuw_postmeta ON dfuw_posts.ID = dfuw_postmeta.post_id 
     WHERE 
      dfuw_posts.post_type = 'poi' 
     AND 
      dfuw_posts.post_status = 'publish' 
     AND 
      dfuw_postmeta.meta_key IN ('_poi_loc' ,'_poi_level' ,'_poi_type')"); 

$pois = array(); 
while($r = mysql_fetch_assoc($results)){ 
    $pois[] = $r; 
} 
echo json_encode($pois);

嘗試發佈您在結束了一下。

+0

這實際上有幫助,但我仍然得到3個單獨的行1個項目。問題是'(dfuw_postmeta.meta_key IN('_poi_loc','_ poi_level','_ poi_type')'使查詢返回1行的3行,而不是3行的所有3個meta_key。 – nmynarcik

+0

您是否嘗試連接它們。希望組也可以工作或嘗試左外連接,所以它不會重複,如果它無法找到第二個表中的任何關聯 –

+0

我試過concat,但我需要組字段在嵌套對象。我的主要問題在我的初始輸出中,請注意前3個對象具有相同的ID,我試圖將這3個合併爲一個對象。 – nmynarcik