gpt4 book ai didi

php - 多个 MySQL 表到 json_encode

转载 作者:可可西里 更新时间:2023-11-01 07:04:33 26 4
gpt4 key购买 nike

我的数据库中有 3 个不同的表,分别是 consoleConsoleconsoleModelconsoleGame。然后我想做的是每个控制台都会有一个内部循环用于它的模型,每个模型都会有另一个内部循环用于它的游戏,如下所示:

[
{
"Console":"PlayStation",
"Information":[
{
"Model":"PlayStation 3",
"Title":[
{
"Game":"007 Legends",
"Publisher":"Electronic Arts"
},
{
"Game":"Ace Combat: Assault Horizon",
"Publisher":"Namco"
}
]
},
{
"Model":"PlayStation 2",
"Title":[
{
"Game":"007: Agent of Fire",
"Publisher":"Electronic Arts"
},
{
"Game":"Ace Combat 4: Shattered Skies",
"Publisher":"Namco"
}
]
},
{
"Model":"PlayStation 1",
"Title":[
{
"Game":"007 Racing",
"Publisher":"Electronic Arts"
},
{
"Game":"Ace Combat",
"Publisher":"Namco"
}
]
}
]
},
{
"Console":"Wii",
"Information":[
{
"Model":"Wii",
"Title":[
{
"Game":"007: Quantum of Solace",
"Publisher":"Activision"
},
{
"Game":"AC/DC Live: Rock Band Track Rack",
"Publisher":"MTV Games"
}
]
}
]
},
{
"Console":"Xbox",
"Information":[
{
"Model":"Xbox",
"Title":[
{
"Game":"AFL",
"Publisher":"Acclaim"
},
{
"Game":"American Chopper",
"Publisher":"Activision"
}
]
},
{
"Model":"Xbox 360",
"Title":[
{
"Game":"AFL Live",
"Publisher":"Electronic Arts"
},
{
"Game":"Akai Katana Shin",
"Publisher":"Cave"
}
]
}
]
}
]

但遗憾的是,我没有在这个数据库中使用我的数据库,而是直接将它写在一个 php 文件中。

编辑

无论如何,继续前进。我修改了我的代码,结果变成了这样。

<?PHP
$consoleQuery = "SELECT * ".
"FROM consoleConsole ".
"JOIN consoleModel ".
"ON consoleConsole.consoleId = consoleModel.consoleId ".
"JOIN consoleGame ".
"ON consoleModel.modelId = consoleGame.gameId";

$consoleResult = mysql_query($consoleQuery);

$consoleFields = array_fill_keys(array(
'consoleName',
), null);

$modelFields = array_fill_keys(array(
'modelName',
), null);

$console = array();
$rowConsole = array();

while ($rowConsole = mysql_fetch_assoc($consoleResult)) {
$consoleId = $rowConsole['consoleId'];
$modelId = $row['modelId'];
if (isset($console[$consoleId]['Information'])) {
$console[$consoleId]['Information'][] = array_intersect_key($rowConsole, $modelFields);
}

else {
$console[$consoleId] = array_intersect_key($rowConsole, $consoleFields);
$console[$consoleId]['Information'] = array(array_intersect_key($rowConsole, $modelFields));
}
}

$console = array_values($console);
echo json_encode($console);

?>

我能够生成一个输出,但它看起来不像上面的输出。

[
{
"consoleName": "PlayStation",
"Information": [
{
"modelName": "PlayStation"
},
{
"modelName": "PlayStation 2"
},
{
"modelName": "PlayStation 3"
},
{
"modelName": "PlayStation 3"
}
]
},
{
"consoleName": "Wii",
"Information": [
{
"modelName": "Wii"
},
{
"modelName": "Wii"
}
]
},
{
"consoleName": "Xbox",
"Information": [
{
"modelName": "Xbox"
},
{
"modelName": "Xbox 360"
}
]
}
]

他们的关系: enter image description here

我现在的问题是,我无法添加每个游戏的标题。

最佳答案

好的,我已经写下了您的解决方案。您必须确保 order by 包含在那里,因为它假定您将它们与那里的项目一起订购。我也不知道您的发布者是如何存储的,所以我将其分离到一个单独的表中(这样您就可以通过那里的发布者获取项目),现在是 4 个连接。另外请注意,我已将其更新为也进行内部连接。这样,对于没有分配任何游戏的控制台,您将不会得到空结果。如果你想要这些,你可以简单地改变连接,这样它也会给你那些结果。让我知道这是否有帮助

//get all of the information
$query = '
SELECT c.consoleId,c.consoleName,m.modelId,m.modelName,g.gameId,g.gameName,p.publisherId,p.publisherName
FROM `consoleconsole` c
INNER JOIN `consolemodel` m ON c.consoleId=m.consoleId
INNER JOIN `consolegame` g ON m.modelId=g.modelId
INNER JOIN `consolepublisher` p ON g.publisherId = p.publisherId
ORDER BY c.consoleName, m.modelName, g.gameName
';

//get the results
$result = mysql_query($query);

//setup array to hold information
$consoles = array();

//setup holders for the different types so that we can filter out the data
$consoleId = 0;
$modelId = 0;

//setup to hold our current index
$consoleIndex = -1;
$modelIndex = -1;

//go through the rows
while($row = mysql_fetch_assoc($result)){
if($consoleId != $row['consoleId']){
$consoleIndex++;
$modelIndex = -1;
$consoleId = $row['consoleId'];

//add the console
$consoles[$consoleIndex]['console'] = $row['consoleName'];

//setup the information array
$consoles[$consoleIndex]['information'] = array();
}

if($modelId != $row['modelId']){
$modelIndex++;
$modelId = $row['modelId'];

//add the model to the console
$consoles[$consoleIndex]['information'][$modelIndex]['model'] = $row['modelName'];

//setup the title array
$consoles[$consoleIndex]['information'][$modelIndex]['title'] = array();
}

//add the game to the current console and model
$consoles[$consoleIndex]['information'][$modelIndex]['title'][] = array(
'game' => $row['gameName'],
'publisher' => $row['publisherName']
);
}

echo json_encode($consoles);

关于php - 多个 MySQL 表到 json_encode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14745588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com