我试图从一个连接的查询中获得所需的内容,并正确地使用值作为数组键,以便正确地构建一些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 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,正如我期望的那样(每个惟一的显示/位置一个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条答案
按热度按时间a0x5cqrl1#
这里的方法是为每个显示行创建一个子数组,以包含所有多个内容记录。
输出: