简体   繁体   中英

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
  • genre: Rock
  • tracks: 13

Can i get this done by getting the index of the album from the URL and use $_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

You really need a unique id to attach to each of your records, to make for easy lookups. (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:

<?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.)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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