2016-12-30 57 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

相关问题