我目前有2个表,两者之间没有建立关系。我只想将第二个表中的数据附加到JSON输出中的第一个表的末尾。
第一桌:
map_type map_value
-----------------------------
image_link www.google.com
top_lat 32.91
right_long -117.13
zoom_level 17.5
第二桌:
marker_name marker_lat marker_long
------------------------------------------
Food 32.91 -117.14
Party 31.22 -119.20
Chips 29.02 -120.00
我理解连接2个表的基本查询,并使用以下代码实现:
$sql = "select * from second_table join first_table";
$q = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$jsonData = array();
while($row = mysqli_fetch_assoc($q))
{
$jsonData[$row['map_type']] = $row['map_value'];
// Issue is here
if ( !isset($jsonData['markers'] ) )
{
$jsonData['markers'] = array();
}
array_push($jsonData['markers'],
array('marker_name'=> $row['marker_name'],
'marker_lat'=> $row['marker_lat'], 'marker_long'=> $row['marker_long']));
}
echo json_encode($jsonData);
因此,我得到以下输出:
{
"image_link": "www.google.com",
"markers": [{
"marker_name": "Food",
"marker_lat": "32.91",
"marker_long": "-117.14"
}, {
"marker_name": "Food",
"marker_lat": "32.91",
"marker_long": "-117.14"
},
...
}],
"top_lat": "32.91",
"right_long": "-117.13",
"zoom_level": "17.5"
}
第二个表中的唯一数据是markers
中包含的数据,而其他字段数据来自第一个表。
问题出现在标有&#34的评论中;问题在这里"。 第二个表格中的每个标记条目重复 4次,其中 4 表示第一个表格中的字段数。
我知道我在那个领域做错了什么,但我刚开始学习并试图更好地理解php。
输出应该类似于,markers
附加到列表末尾,并且条目数正确:
{
"image_link": "www.google.com",
"top_lat": "32.91",
"right_long": "-117.13",
"zoom_level": "17.5",
"markers": [{
"marker_name": "Food",
"marker_lat": "32.91",
"marker_long": "-117.14"
}, {
"marker_name": "Party",
"marker_lat": "31.22",
"marker_long": "-119.20"
}, {
"marker_name": "Chips",
"marker_lat": "29.02",
"marker_long": "-120.00"
}]
}
如何实现此输出?
更新:正如@sepehr所解释的那样,我现在了解原因,并将代码替换为以下内容以进行2次查询:
$sql = "select * from first_table";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$jsonData = array();
while($row =mysqli_fetch_assoc($result))
{
$jsonData[$row['map_type']] = $value;
}
$sql = "select * from second_table";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$index = 1;
while($rownew =mysqli_fetch_assoc($result))
{
$markers[$index] = array("marker_name" => $rownew['marker_name'], "marker_lat" => doubleval($rownew['marker_lat']), "marker_long" => doubleval($rownew['marker_long']) );
++$index;
}
$jsonData['markers'] = $markers;
echo json_encode ($jsonData);
这实现了正确的输出,但有人可以告诉我这是否是最有效和最佳的方式?
答案 0 :(得分:1)
这些重复是因为{
"@search.score": 0.5696786,
"Guid": "ce73ca06-f170-46df-b0ef-a6e6e72b76ce",
"FirstName": "Ruy",
"LastName": "Bssaf",
"Phone": "560523791699",
"CustomerId": "-1",
"CustomerEmail": "guy@twingocoil",
"MySuperpharm": "True"
},
{
"@search.score": 0.5619051,
"Guid": "090c623f-5993-458e-93cc-8ef3d885eb29",
"FirstName": "ruy",
"LastName": "reffen",
"Phone": "0522545833",
"CustomerId": "76016443160",
"CustomerEmail": "guy@geffenmedicalcom",
"MySuperpharm": "False"
},
语句。它会产生重复的行。 Have a look为你自己。
为了构建那个json对象,我建议你对这些表执行两个单独的查询。因为它们彼此之间没有逻辑关系。
或者,如果您必须在一个我不建议的查询中实现此目的,use a UNION
like this以避免这些重复记录。
答案 1 :(得分:0)
分别从2个表中获取结果,并使用<button type="button">
<img src="http://img.jpg">
</button>
<button type="button">
<img src="http://img2.jpg">
</button>
<button type="button">
<img src="http://img3.jpg">
</button>
Select images: <input type="file" name="img" multiple>
<input type="submit">
函数合并它们。
array_merge