简体   繁体   English

详情页多维数组

[英]Detail page multidimensional array

I have a multidimensional array with albums我有一个带专辑的多维数组

$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'
        ]
    ];
?>

And i have them in a table我把它们放在一张桌子上

<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">&copy; 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>

I want to get the data of one specific album on the detail page and display them in a list on the detailpage.我想在详细信息页面上获取一个特定专辑的数据,并将它们显示在详细信息页面的列表中。

like this:像这样:

  • artist: Muse艺术家:缪斯
  • album: Live At Rome Olympic Stadium专辑:Live At Rome Olympic Stadium
  • genre: Rock类型:摇滚
  • tracks: 13曲目:13

Can i get this done by getting the index of the album from the URL and use $_GET?我可以通过从 URL 获取专辑索引并使用 $_GET 来完成此操作吗?

update your link like更新您的链接,例如

<td><a href="details.php?album_id=<?= $index ?>">Detail</a></td> 

and use the following code on the detail page并在详细信息页面上使用以下代码

$album_id = $_GET['album_id'];
echo isset($musicAlbums[$album_id]['artist']) ? $musicAlbums[$album_id]['artist'] : '-';

but again this is not the best way to do this you need to use some unique id or slug to show details但这又不是最好的方法,您需要使用一些唯一的 id 或 slug 来显示详细信息

You really need a unique id to attach to each of your records, to make for easy lookups.您确实需要一个唯一的 ID 来附加到您的每条记录,以便于查找。 (You could use your existing numerical indexes, but if the order of your records change that might present an issue.) (您可以使用现有的数字索引,但如果您的记录顺序发生变化,则可能会出现问题。)

Here is an approach that hashes each array value using md5 and serialize, we can then use that as an identifier/key for a later lookup, and link:这是一种使用 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]);
}

Output:输出:

<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',
)

An issue might be, that switching the order of an album array or any value within, will change the hash.一个问题可能是,切换专辑数组的顺序或其中的任何值都会改变哈希。

Also as the list increases, you may not want to hash a long list each time.此外,随着列表的增加,您可能不想每次都散列一个长列表。 (You could cache the list and/or dump with var_export.) (您可以使用 var_export 缓存列表和/或转储。)

It's likely you'll graduate to a database at some point.您很可能会在某个时候升级到数据库。 If you do so, you could add a unique id field instead of the hash above, which you could use to do a similar job.如果你这样做,你可以添加一个唯一的 id 字段而不是上面的哈希,你可以用它来做类似的工作。

An alternative to associating a unique id for each album, would be to use something like an artist/album name combination as a lookup, and then to search/filter your existing list.为每个专辑关联唯一 ID 的另一种方法是使用艺术家/专辑名称组合之类的内容作为查找,然后搜索/过滤现有列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM