[英]Detail page multidimensional array
我有一個帶專輯的多維數組
$musicAlbums =
[
// fill the collection with albums (also arrays)
[
'artist' => 'Anderson Paak',
'album' => 'Ventura',
'genre' => 'hip-hop',
'year' => '2016',
'tracks' => '5'
],
[
'artist' => 'Muse',
'album' => 'Simulation',
'genre' => 'Rock',
'year' => '2018',
'tracks' => '12'
],
[
'artist' => 'Limp Bizkit',
'album' => 'Gold Cobra',
'genre' => 'Rock / Rap',
'year' => '2011',
'tracks' => '16'
],
[
'artist' => '30 Seconds To Mars',
'album' => 'This Is War',
'genre' => 'Rock',
'year' => '2009',
'tracks' => '15'
]
];
?>
我把它們放在一張桌子上
<body>
<h1>Music Collection</h1>
<table>
<thead>
<tr>
<th>#</th>
<th>Artist</th>
<th>Album</th>
<th>Genre</th>
<th>Year</th>
<th>Tracks</th>
<th></th>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="6">© My Collection</td>
</tr>
</tfoot>
<tbody>
<?php foreach ($musicAlbums as $index => $album) { ?>
<tr>
<td><?= $index + 1 ?></td>
<td><?= $album['artist'] ?></td>
<td><?= $album['album'] ?></td>
<td><?= $album['genre'] ?></td>
<td><?= $album['year'] ?></td>
<td><?= $album['tracks'] ?></td>
<td><a href="details.php?">Detail</a></td> //here needs to come the detail page link
</tr>
<?php } ?>
</tbody>
</table>
</body>
我想在詳細信息頁面上獲取一個特定專輯的數據,並將它們顯示在詳細信息頁面的列表中。
像這樣:
我可以通過從 URL 獲取專輯索引並使用 $_GET 來完成此操作嗎?
更新您的鏈接,例如
<td><a href="details.php?album_id=<?= $index ?>">Detail</a></td>
並在詳細信息頁面上使用以下代碼
$album_id = $_GET['album_id'];
echo isset($musicAlbums[$album_id]['artist']) ? $musicAlbums[$album_id]['artist'] : '-';
但這又不是最好的方法,您需要使用一些唯一的 id 或 slug 來顯示詳細信息
您確實需要一個唯一的 ID 來附加到您的每條記錄,以便於查找。 (您可以使用現有的數字索引,但如果您的記錄順序發生變化,則可能會出現問題。)
這是一種使用 md5 散列每個數組值並序列化的方法,然后我們可以將其用作以后查找的標識符/鍵,並鏈接:
<?php
$musicAlbums =
[
[
'artist' => 'Anderson Paak',
'album' => 'Ventura',
'genre' => 'hip-hop',
'year' => '2016',
'tracks' => '5'
],
[
'artist' => 'Muse',
'album' => 'Simulation',
'genre' => 'Rock',
'year' => '2018',
'tracks' => '12'
]
];
$hashes = array_map('md5', array_map('serialize', $musicAlbums));
// Replace the original indexes with the hashes generated above
// for each array entry.
$hash_values = array_combine($hashes, $musicAlbums);
// Your HTML list of links, edited for brevity, hash used as link.
foreach($hash_values as $hash => $value) {
echo '<a href="?hash=', $hash, '">', $value['album'], '</a><br>', "\n";
}
// Individual album/item.
$hash = $_GET['hash'] ?? null;
$hash = 'f0531700a9fab31c02d2a5a211eafe67'; // Hard coded override here for example output
if($hash && isset($hash_values[$hash])) {
var_export($hash_values[$hash]);
}
輸出:
<a href="?hash=66f0ff103b5c7b512f80e7e836357078">Ventura</a><br>
<a href="?hash=f0531700a9fab31c02d2a5a211eafe67">Simulation</a><br>
array (
'artist' => 'Muse',
'album' => 'Simulation',
'genre' => 'Rock',
'year' => '2018',
'tracks' => '12',
)
一個問題可能是,切換專輯數組的順序或其中的任何值都會改變哈希。
此外,隨着列表的增加,您可能不想每次都散列一個長列表。 (您可以使用 var_export 緩存列表和/或轉儲。)
您很可能會在某個時候升級到數據庫。 如果你這樣做,你可以添加一個唯一的 id 字段而不是上面的哈希,你可以用它來做類似的工作。
為每個專輯關聯唯一 ID 的另一種方法是使用藝術家/專輯名稱組合之類的內容作為查找,然后搜索/過濾現有列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.