我开始学习php,我编写了2个查询来从两个表中获取数据。
目前,这些是我的表格:
food_vendors
ID Name Description
-----------------------------
1 Vendor 1 testing
2 Vendor 2 testing
3 Vendor 3 testing
food_vendor_menu
ID VENDOR_ID FOOD_NAME
-----------------------------
1 1 Food 1
2 1 Food 2
3 2 Food 3
4 2 Food 4
5 3 Food 5
food_vendor_menu 中的每个vendor_id
都对应 food_vendors 中的id
。因此,供应商1将有2个食品,以及供应商2,而供应商3仅有1个食品。
目前,我正在制作2个查询并循环浏览它们:
$sql = "select * from food_vendors";
$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 food_vendor_menu where vendor_id=" .$row['id']. "" ;
$resultnew = mysqli_query($connection, $sqlnew) or die("Error in Selecting " . mysqli_error($connection));
$jsonData = array();
$rowCountnew = $resultnew->num_rows;
$indexnew = 1;
$menuStrings = array();
if ($rowCountnew >0)
{
while($rownew =mysqli_fetch_assoc($resultnew))
{
$menuStrings[$indexnew] = array("id" => $rownew['id'], "food_name" => $rownew['food_name']);
++$indexnew;
}
}
echo '"item'.$index.'":';
echo json_encode(array("id" => intval($row['id']), "name" => $row['name'], "description" => $row['description'], "menu_items" =>$menuStrings));
if ($rowCount != $index)
{
echo ',';
}
++$index;
}
echo ' }';
这会产生以下输出:
{
"item1": {
"id": 1,
"name": "Vendor 1",
"description": "testing":,
"menu_items": {
"1": {
"id": "1",
"food_name": "Food 1"
},
"2": {
"id": "2",
"food_name": "Food 2"
}
}
},
"item2": {
"id": 2,
"name": "Vendor 2",
"description": "testing"
"menu_items": {
"1": {
"id": "3",
"food_name": "Food 3"
},
"2": {
"id": "4",
"food_name": "Food 4"
}
}
},
"item3": {
"id": 3,
"name": "Vendor",
"description": "testing",
"menu_items": {
"1": {
"id": "5",
"food_name": "Food 5",
}
}
}
}
但是,我不相信这是最好的方式,而且我只想使用一个查询来生成相同输出,所以经过研究,我尝试学习并替换上面的内容代码如下,将2个表连接在一起:
$sql = "select * from food_vendor_menu s join food_vendors t on t.id=vendor_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( $row['vendor_id'] ) )
{
$index = 'item'.$row['vendor_id'].'';
$jsonData[$index] = $row;
unset( $jsonData[$index]['food_name'] );
$jsonData[$index]['menu_items'] = array();
$jsonData[$index]['menu_items'] = $row['food_name'];
}
}
echo json_encode($jsonData);
输出结果:
{
"item1": {
"id": "1",
"vendor_id": "1",
"name": "Vendor 1",
"description": "testing",
"menu_items": "Food 1"
},
"item2": {
"id": "2",
"vendor_id": "2",
"name": "Vendor 2",
"description": "testing",
"menu_items": "Food 3"
},
"item3": {
"id": "3",
"vendor_id": "3",
"name": "Vendor 3",
"description": "testing",
"menu_items": "Food 5"
}
}
我知道这是更好的方法,效率更高,但正如您从上面的输出中看到的那样,供应商1的menu_items
数组只有Food 1
,但没有Food 2
。与其他2家供应商的问题相同。此外,我需要将vendor_id
附加到每个菜单项,而不仅仅是食物名称。
我知道问题出在哪里,由于我目前对php的了解有限,我只是不知道如何修复它。
有人可以协助吗?
更新
按照乔伊的建议,我换了:
$jsonData[$index] = $row
$jsonData[$index]['menu_items'] = array();
$jsonData[$index]['menu_items'] = $row['food_name'];
使用:
if ( !isset( $jsonData[$index] ) )
{
$jsonData[$index] = $row;
$jsonData[$index]['menu_items'] = array();
}
array_push($jsonData[$index]['menu_items'], $row['food_name']);
我得到以下输出:
"item1": {
"id": "1",
"name": "Vendor 1",
"description": "testing",
"menu_items": ["Food 1", "Food 2"]
}
但是,我需要将每个Food 1
,Food 2
作为一个数组本身,因为我需要将其他数据与每个食品相关联,例如id
,食品价格等。< / p>
答案 0 :(得分:1)
$sql = "select * from food_vendor_menu s join food_vendors t on t.id=vendor_id";
...
$jsonData[$index] = $row
...
$jsonData[$index]['menu_items'] = array();
$jsonData[$index]['menu_items'] = $row['food_name'];
应该是这样的:
$sql = "select s.id as food_id, s.food_name, s.vendor_id, t.name, t.description from food_vendor_menu s join food_vendors t on t.id=vendor_id";
...
if (!isset($jsonData[$index])) {
$jsonData[$index] = $row;
unset( $jsonData[$index]['food_id'], $jsonData[$index]['food_name'] );
$jsonData[$index]['menu_items'] = array();
}
array_push($jsonData[$index]['menu_items'],
array('id'=>$row['food_id'], 'food_name'=>$row['food_name']));
答案 1 :(得分:1)
查询:
fmap concat (etc)
Php处理
$sql = "
SELECT
t.*,
GROUP_CONCAT(CONCAT(s.id, '%%',s.food_name) SEPARATOR '||') as menu_items
FROM
food_vendor_menu s
JOIN
food_vendors t
ON
t.id=vendor_id
GROUP BY
s.vendor_id
";
我认为menu_items以数组形式(包含在[]中)而不是对象(包含在{}中)表示更好。