从SQL查询重组数组,以便我可以回显正确的值

时间:2018-09-12 14:05:15

标签: 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 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(每个唯一的显示/位置一个) 但是我需要弄清楚如何正确安排它,以便可以将DIsplay名称呼应为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 个答案:

答案 0 :(得分:1)

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

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