[英]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.