Hey

I have an array which pulls in the name, artist and URL of songs from a playlist.xml file. The array prints like the following:

Array ( [0] => Array ( [tag] => PLAYLIST [value] => Array ( [0] => Array ( [tag] => SONG [value] => [attributes] => Array ( [URL] => audio/track1.mp3 [TITLE] => Song 1 [ARTIST] => Artist 1 ) ) [1] => Array ( [tag] => SONG [value] => [attributes] => Array ( [URL] => audio/track2.mp3 [TITLE] => Song 2 [ARTIST] => Artist 2 ) ) [2] => Array ( [tag] => SONG [value] => [attributes] => Array ( [URL] => audio/track3.mp3 [TITLE] => Song 3 [ARTIST] => Artist 3 ) ) ) ) )

I need to read the array and print each of the songs as a link in the following way:

<a href='SONG URL'>SONG TITLE - SONG ARTIST</a>

Any help would be greatly appreciated.

Thanks

Recommended Answers

All 4 Replies

Hey

I have an array which pulls in the name, artist and URL of songs from a playlist.xml file. The array prints like the following:

Array ( [0] => Array ( [tag] => PLAYLIST [value] => Array ( [0] => Array ( [tag] => SONG [value] => [attributes] => Array ( [URL] => audio/track1.mp3 [TITLE] => Song 1 [ARTIST] => Artist 1 ) ) [1] => Array ( [tag] => SONG [value] => [attributes] => Array ( [URL] => audio/track2.mp3 [TITLE] => Song 2 [ARTIST] => Artist 2 ) ) [2] => Array ( [tag] => SONG [value] => [attributes] => Array ( [URL] => audio/track3.mp3 [TITLE] => Song 3 [ARTIST] => Artist 3 ) ) ) ) )

I need to read the array and print each of the songs as a link in the following way:

<a href='SONG URL'>SONG TITLE - SONG ARTIST</a>

Any help would be greatly appreciated.

Thanks

I think this should work:

<?php
//$arrSongList = your array

foreach($arrSongList as $playlist)
{
	foreach($playlist["value"] as $song)
	{
		$url = isset($song["attributes"]["URL"]) && trim($song["attributes"]["URL"]) != ""?$song["attributes"]["URL"]:false;
		$title = isset($song["attributes"]["TITLE"]) && trim($song["attributes"]["TITLE"]) != ""?$song["attributes"]["TITLE"]:false;
		$artist = isset($song["attributes"]["ARTIST"]) && trim($song["attributes"]["ARTIST"]) != ""?$song["attributes"]["ARTIST"]:false;
		
		if($url && $title && $artist) echo '<a href="' . $url . '">' . $title . ' - ' . $artist . '</a>';
	}
}
?>

Thankyou very much :) Works perfectly!

I wonder if you could help with another thing....
I need to be able to delete individual songs from the XML file on request.
So I'd print out a button in the for loop obviously.... but Im not sure how to go around deleting the actual text from the XML file itself.
Any ideas?

Thanks

Thankyou very much :) Works perfectly!

I wonder if you could help with another thing....
I need to be able to delete individual songs from the XML file on request.
So I'd print out a button in the for loop obviously.... but Im not sure how to go around deleting the actual text from the XML file itself.
Any ideas?

Thanks

This is going to be a little complicated if your experience in arrays and xml is limited but it will be a good accomplishment. You need to look into extracting the xml into an array. There are several libraries and classes as well as examples available on how to do so. Once you have figured out how to do this you can then loop through the array rewriting the xml file while avoiding specific values(which will be your deleted songs).

But first thing's first, find a way of extracting the xml into an array. If you search on google for "php xml to array" you will find several prewritten functions or classes that will do this for you. Once you have that done come back and I will help you in rewriting the file.

Hi OS_DEV

I've already got the xml in a php array. As seen in my code example above and it works perfectly for showing what songs are currently in the playlist.
However, it is specifically the part of looping through a deleting the song from the xml which I do not understand. I'd really appreciate it if you could help. I've tried researching but I didn't understand much of it and nothing was nearly relevant to my needs.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.