2016-12-30 61 views
2

我是新來的PHP,並試圖實現一種方式來從一個表輸出數據,以對應於另一個表中的JSON對象。表中的項目與另一個表中的特定JSON不匹配:PHP?

目前這是主要的表:

enter image description here

這是含有是假設在主表對應於每個「ID」項所述第二表:

enter image description here

例如,在第二個表中,culturevillage_id對應於主表中的id。因此,使用id = 1的JSON應輸出6個項目。

這是我試圖執行代碼:

$sql = "select * from main_table"; 

$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection)); 

$jsonData = array(); 
$rowCount = $result->num_rows; 
$index = 1; 

while($row =mysqli_fetch_assoc($result)) 
{ 
    $sqlnew = "select * from secondary_table where culturevillage_id=" .$row['id']. "" ; 
    $resultnew = mysqli_query($connection, $sqlnew) or die("Error in Selecting " . mysqli_error($connection)); 

    //create an array 
    $jsonData = array(); 
    $rowCountnew = $resultnew->num_rows; 
    $indexnew = 1; 
    //echo $rowCount; 
    if ($rowCountnew >0) 
    { 
     while($rownew =mysqli_fetch_assoc($resultnew)) 
     { 
      $imageString[$indexnew] = array("normal_res_link" => $rownew['normal_res_link']); 

      ++$indexnew; 

     } 
    } 

    echo '"item'.$index.'":'; 
    echo json_encode(array("id" => intval($row['id']),"english_name" => $row['english_name'], "vietnamese_name" => $row['vietnamese_name'], "images" =>$imageString)); 
    if ($rowCount != $index) 
    { 
     echo ','; 
    } 
    ++$index; 
} 

echo ' }' 

不過,我得到以下輸出:

{ 
    "item1": { 
     "id": 1, 
     "english_name": "Ben Thanh Market", 
     "vietnamese_name": "Cho Ben Thanh", 
     "images": { 
      "1": { 
       "normal_res_link": "id=1" 
      }, 
      "2": { 
       "normal_res_link": "id=1" 
      }, 
      "3": { 
       "normal_res_link": "id=1" 
      }, 
      "4": { 
       "normal_res_link": "id=1" 
      }, 
      "5": { 
       "normal_res_link": "id=1" 
      }, 
      "6": { 
       "normal_res_link": "id=1 
      } 
     } 
    }, 
    "item2": { 
     "id": 3, 
     "english_name": "One Pillar Pagoda", 
     "vietnamese_name": "Chua Mot Cot", 
     "images": { 
      "1": { 
       "normal_res_link": "id=1" 
      }, 
      "2": { 
       "normal_res_link": "id=1" 
      }, 
      "3": { 
       "normal_res_link": "id=1" 
      }, 
      "4": { 
       "normal_res_link": "id=1" 
      }, 
      "5": { 
       "normal_res_link": "id=1" 
      }, 
      "6": { 
       "normal_res_link": "id=1" 
      } 
     } 
    }, 
    "item3": { 
     "id": 4, 
     "english_name": "Hung King Temple", 
     "vietnamese_name": "Den Hung" 
     "images": { 
      "1": { 
       "normal_res_link": "id=4" 
      }, 
      "2": { 
       "normal_res_link": "id=4" 
      }, 
      "3": { 
       "normal_res_link": "id=1" 
      }, 
      "4": { 
       "normal_res_link": "id=1"" 
      }, 
      "5": { 
       "normal_res_link": "id=1" 
      }, 
      "6": { 
       "normal_res_link": "id=1" 
      } 
     } 
    } 
} 

item1 JSON的images領域是正確的,應該有6個項目。但對於item2,輸出從item1的圖像數據中獲取,而對於item3,則從item1的圖像數據中獲取剩餘的4個項目。

正確的輸出應該是item2的圖像字段應該是空的,item3的圖像字段應該只包含2個數據,根據輔助表中的數據。

我知道我的實現是錯誤的地方,但我不確定。

有人能指出我正確的方向嗎?

+0

你爲什麼不在這裏使用JOIN? –

回答

1

你只是沒有初始化你的​​數組;

添加$imageString = array();之前任何推動它。

$imageString = array(); // <-- 
if ($rowCountnew >0) 
{ 
    while($rownew =mysqli_fetch_assoc($resultnew)) 
    { 

Offtopic:

  1. 還嘗試以下查詢:

    select s.* from secondary_table s join main_table t on t.id=s.culturevillage_id

  2. 你應該使用json_encode作爲整個數據,而不是嘗試按部分生成它。


$sql = "select * from secondary_table s join main_table t on t.id=s.culturevillage_id" ; 
$q = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection)); 

//create an array 
$jsonData = array(); 
while($row =mysqli_fetch_assoc($q)) 
{ 
    if (!isset($jsonData[$q['culturevillage_id']]) { 
    $jsonData[$q['culturevillage_id']] = $row; 
    unset($jsonData[$q['culturevillage_id']]['normal_res_link']); 
    $jsonData[$q['culturevillage_id']]['images'] = array(); 
    } 
    $jsonData[$q['culturevillage_id']]['images'][] = $row['normal_res_link']; 
} 
echo json_encode($row); 
+0

我添加了建議的代碼,它似乎部分工作.'item3'不輸出id = 1的項目,但是'item2'仍然輸出6個項目,即使它應該是空的。 – Pangu

+0

只需將其提升到合適的位置。 –

+0

對不起,我對此有點新,只是學習並不確定你的意思,我用secondary_table替換了'$ sqlnew =「select * from culturevillage_id =」。$ row ['id']。 「';'帶''sqlnew =」select s。* from secondary_table s join main_table t on t.id = s.culturevillage_id =「。$ row ['id']。 「」;'但現在沒有'item3'和'item3'的數據輸出連接到'item1' – Pangu

1

我有不同的看法,你已經使用了一個循環,從一個表中獲取每一個ID,然後你正在嘗試做同樣的另一個查詢。在創建表時,您需要在sql中使用FOREIGN_KEY,然後您將只能在一個查詢中進行操作,而無需任何循環。例如,我通過這個創建了一個消息系統。

你可以看看這個

這些表,你可以看到連接 - enter image description here

現在檢查出這一個,你這是怎麼創建多個表 -

外鍵

enter image description here

現在當你必須爲它寫一個SQL查詢你會寫它謊言這 -

enter image description here

您可以看到此查詢在一個查詢中使用三個表作爲參考。

+1

當你想要實現兩個以上的用戶對話時,這個「對話」表將會受到傷害。使用'(cr_id,user_id,...)'結構 –

+1

[示例](https://habrastorage.org/files/5c5/edd/69a/5c5edd69a5ae451c81c314f2525ecad6.png) –

+0

@使用另一個'conversation_participants'表要好得多@ vp_arth非常感謝你,但我只是在幫助外鍵。但謝謝你的解決方案:) – uniqueNt

相關問題