从sql查询中重新构造数组,以便可以回显正确的值

gwbalxhn  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(254)

我试图从一个连接的查询中获得所需的内容,并正确地使用值作为数组键,以便正确地构建一些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;?>
a0x5cqrl

a0x5cqrl1#

这里的方法是为每个显示行创建一个子数组,以包含所有多个内容记录。

// 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
------------

相关问题