Jump to content

Outputting an additional PW field in an array. How?


n0sleeves
 Share

Recommended Posts

Not sure how to ask this, but basically I have an associative PHP array outputting a song ID and it's lyrics from PW using this code: It's being stored in the $lyricsArray variable



<?php // Loop though album Tracks

$numTracksContainingLyrics = 0; // How many contain lyrics?
$lyricsArray = array(); // Create empty array to hold lyrics

foreach ($albumTracks as $track) {
$html = "<tr>";
$html .= "<td>";
$html .= "<a href='" . $track->song_file->url . "' class=''>" . $track->title . "</a>";
$html .= "</td>";

$html .= "<td>";
if ($track->lyrics != null) { // If lyrics field isn't empty
$html .= '<a href="#" class="lyricCLicked" data-reveal-id="songID_' . $track->id . '">Lyrics</a>';
$numTracksContainingLyrics++;
foreach ($track as $value) {
$lyricsArray[$track->id] = $track->lyrics;
}
};
$html .= "</td>";

$html .= "<td>";
$html .= "<a href='#'>Buy</a>";
$html .= "</td>";
$html .= "</tr>";

echo $html;
}

?>


The PHP code for outputting those values mentioned above is below: What I'd like to do is also output the song title (commented in  red in the code below). However, I don't know how to go about doing it using the simple arrays I have been constructing. 



<?php
foreach ($lyricsArray as $key => $value) {
$lyricModal = '<div id="songID_' . $key .'" class="reveal-modal small" data-reveal>';
$lyricModal = '<h2>' . //HOW DO I ALSO GET $track-title HERE??? . '</h2>';
$lyricModal .= $value;
$lyricModal .= '<a class="close-reveal-modal">×</a>';
$lyricModal .= '</div>';

echo $lyricModal;
}
?>


Thank you for your help!

Link to comment
Share on other sites

When you're building your array, make it multidimensional. Something like this should do the trick:

$lyricsArray[$track->id]['title'] = $track->title;
$lyricsArray[$track->id]['lyrics'] = $track->lyrics;

Then when you a are foreach'ing it, you can:

foreach ($lyricsArray as $track) {
    $lyricModal .= $track['title'];
    $lyricModal .= $track['lyrics'];
}

But then I am also wondering why you are converting the $track object, which already has title and lyrics, into an associative array. Is it just for the lyrics empty check?

If so, then you can do that by foreaching the albumTracks object again:

foreach ($albumTracks as $track) {
    if ($track->lyrics != null) {
        $lyricModal = '<div id="songID_' . $track->id .'" class="reveal-modal small" data-reveal>';
        $lyricModal = '<h2>' . $track->title . '</h2>';
        $lyricModal .= $track->lyrics;
        $lyricModal .= '<a class="close-reveal-modal">×</a>';
        $lyricModal .= '</div>';
        echo $lyricModal;        
    }
}

Does that do what you want, or am I missing something :)

  • Like 1
Link to comment
Share on other sites

Oh my goodness, I'm such a dope!! Why was I over-complicating it!???  :'( That indeed does work and does so very simply! Thank you!!!  :)  :)  :) 
I was probably converting the $track object again because I am a noob and a bad one at that! This is the second time I have seemed to over-complicate / over-think something in PHP. Did you ever experience this symptom when learning the language or while beginning programming? 

Super embarrassing, but so grateful. Thanks for not punishing me and still taking the time to show / teach me the correct code. Y


 May I ask something (since you pointed it out)?  In the books I am reading, I haven't seen creating multi-dimensional arrays like the way you are showing below. I am interested if you could explain that part of the code in more detail please? Particularly, the second set of brackets? Is this a shortcut to creating such an array?

$lyricsArray[$track->id]['title'] = $track->title;
$lyricsArray[$track->id]['lyrics'] = $track->lyrics;

Many thanks!!

Link to comment
Share on other sites

Don't worry about overcomplicating - I think everyone still does that at times when learning something new. There are so many ways of doing something and it often takes time and reflection to realize the simplest way, especially when the goals of your site or app change as you go along. 

That code I gave would produce a multi-dimensional array something like this. So for each track id, you'll have available both the title and the lyrics as separate key/value pairs that you can access.

Array (

    [156] => Array (
         [title] => Song Title
         [lyrics] => This is the way we code, the way we code, the way we code
    )

)

Link to comment
Share on other sites

 Share

×
×
  • Create New...