Php 从sql查询重新构造数组,以便可以回显正确的值
我试图从连接查询中获取所需的内容,并正确地使用值作为数组键,以便正确地构建一些DIV列表 我的php查询和数组:Php 从sql查询重新构造数组,以便可以回显正确的值,php,html,mysql,arrays,loops,Php,Html,Mysql,Arrays,Loops,我试图从连接查询中获取所需的内容,并正确地使用值作为数组键,以便正确地构建一些DIV列表 我的php查询和数组: $getTickers = " SELECT d.ID as displayID, d.display_name as display, l.location_name as locationName, d.location_id as location, t.id as ticker, tc.id as contentID, tc.content FROM
$getTickers = "
SELECT d.ID as displayID, d.display_name as display, l.location_name as locationName, d.location_id as location, t.id as ticker, tc.id as contentID, tc.content
FROM displays d
INNER JOIN locations l on d.location_id = l.id
INNER JOIN tickers t on d.id = t.display_id
INNER JOIN ticker_content tc on t.id = tc.ticker_id;
";
$tickers = $mysqlConn->query($getTickers);
$tickerDisplays = array();
foreach($tickers as $subArray) {
if(!array_key_exists($subArray['displayID'], $tickerDisplays)) {
$tickerDisplays[$subArray['displayID']] = array();
}
// here you add `display_name` under key `display_id`
$tickerDisplays[$subArray['displayID']][$subArray['location']] = $subArray['display'];
}
下面的所有示例和代码,但我不需要html结构的帮助,只需要如何重新构造数组/键以获得所需的结果,以及如何在前端循环它们
我现在得到了我期望的4个div(每个独特的显示/位置一个)
但是我需要弄清楚如何正确地排列它,这样我就可以将显示名称回显为h4,位置回显为h5,然后是列表中的每个内容
所以查询结果告诉我:
displayID | display | locationName | location | ticker | contentID | content |
1 Office Building 4 4 1 1 testing content
2 Lobby Building 4 4 2 2 testing content 2
3 Lobby Building 1 1 3 3 testing content 3
4 Office Building 1 1 4 4 testing content 4
4 Office Building 1 1 4 5 testing content again
我试图循环使用预期结果,即每个位置/显示组合都有一个div,如下所示:
OFFICE
Building 4
testing content
---------------
LOBBY
Building 4
testing content 2
------------------
LOBBY
Building 1
testing content 3
------------------
OFFICE
Building 1
testing content 4
testing content again
----------------------
这是我目前试图循环的方式
<?php foreach($tickerDisplays as $key => $ticker):?>
<h4><?php echo $key ?></h4> //so this should be the display Name (office, lobby)
<h5><?php echo //location?></h5> //this should be the location name (Building 1, Building 4)
//This will be another foreach for any content associated with the location/display
<ul class="tickerContent">
<li>
</ul>
</div>
</div>
<?php endforeach;?>
//所以这应该是显示名称(办公室、大厅)
//这应该是位置名称(1号楼、4号楼)
//这将是与位置/显示相关的任何内容的另一个foreach
-
这里的方法是为每个显示行创建一个子数组,以包含所有
多个内容记录
// Dummy the data from the query
$tickers = [
['displayID' => 1, 'display' => 'Office', 'locationName' => 'Building 4', 'location' => 4, 'ticker' => 1, 'contentID' => 1, 'content' => 'testing content'],
['displayID' => 2, 'display' => 'Lobby', 'locationName' => 'Building 4', 'location' => 4, 'ticker' => 2, 'contentID' => 2, 'content' => 'testing content 2'],
['displayID' => 3, 'display' => 'Lobby', 'locationName' => 'Building 1', 'location' => 1, 'ticker' => 3, 'contentID' => 3, 'content' => 'testing content 3'],
['displayID' => 4, 'display' => 'Office', 'locationName' => 'Building 1', 'location' => 1, 'ticker' => 4, 'contentID' => 4, 'content' => 'testing content 4'],
['displayID' => 4, 'display' => 'Office', 'locationName' => 'Building 1', 'location' => 1, 'ticker' => 4, 'contentID' => 5, 'content' => 'testing content again']
];
// A place to keep the reorganized data
$tickerDisplays = [];
// Walk through the query result
foreach($tickers as $row) {
$displayID = $row['displayID']; // for convenience and readability
$location = $row['location']; // for convenience and readability
$display = $row['display'];
$contentID = $row['contentID'];
if ( ! array_key_exists($row['displayID'], $tickerDisplays) ) {
$tickerDisplays[$displayID] = [
'displayID' => $row['displayID'],
'display' => $row['display'],
'ticker' => $row['ticker'],
'contentID' => $row['contentID'],
'content' => $row['content'],
'location' => $row['location'],
'locationName' => $row['locationName'],
'@content' => [] // to store the content data
];
}
$tickerDisplays[$displayID]['@content'][$contentID] = ['content' => $row['content']];
}
print_r($tickerDisplays);
foreach ( $tickerDisplays as $key => $ticker ) {
// Output the display and location name
out($ticker['display']);
out($ticker['locationName']);
// Output all the content records.
foreach ( $ticker['@content'] as $contentID => $content ) {
out($content['content']);
}
out('------------');
}
// Just a utility function
function out($msg) {
echo "$msg\n";
}
输出:
Office
Building 4
testing content
------------
Lobby
Building 4
testing content 2
------------
Lobby
Building 1
testing content 3
------------
Office
Building 1
testing content 4
testing content again
------------
我由此猜测,由于连接,displayID将多次出现在查询结果中?是的,这是正确的,因为多条内容记录将分配给一个Display。如果您可以将查询结果的原始转储添加到问题中,这将非常有帮助。上面的查询结果是实际结果,您的意思是希望从workbench获得转储?从
$mysqlConn->query($getTickers)获得的原始转储代码>这是你在问题中发布的表格吗?如果是这样,您可以为列添加标签。我不想假设。我想这正是我想要的,谢谢!