PeterBrinn Posted January 14, 2011 Share Posted January 14, 2011 Working with a now playing module for some music. My music player outputs this kind of XML (WHEN THE SONG IS PLAYING): <?xml version="1.0" encoding="utf-8" ?> - <Playing station="Station"> - <item type="MUS"> <Artist>Beatles</Artist> <SongTitle>Get Back</SongTitle> </item> </Playing> My music player outputs this kind of XML (WHEN THE SONG IS NOT PLAYING): <?xml version="1.0" encoding="utf-8" ?> - <Playing station="Station"> - <item type="END"> <Artist>MORE MUSIC SOON</Artist> <SongTitle /> </item> </Playing> Here's my PHP: <?php $playing = simplexml_load_file("../playlist/nowplaying.xml"); foreach ($playing->item as $item) { printf("Now playing: %s\n", $item->SongTitle); printf("by %s\n", $item->Artist); } ?> I want to make my php document check the item TYPE. (item type="x"). If x equals "MUS", display the PHP like above. But if x displays "END", I want it to display something like "More music coming up". Right now, it displays "MORE MUSIC SOON by " I've attempted if statements to no avail, mainly because I'm not super familiar with them in PHP. Ideas? Link to comment https://forums.phpfreaks.com/topic/224445-simplexml-if-statement/ Share on other sites More sharing options...
PeterBrinn Posted January 14, 2011 Author Share Posted January 14, 2011 EDIT: Solved! My code was too robust. So I simplified it! Here's what I did. <?php $playing = simplexml_load_file("../playlist/nowplaying.xml"); if($playing->item['type'] == "MUS"){ printf("Now playing: %s\n", $playing->item ->SongTitle); printf("by %s\n", $playing->item ->Artist); } else printf("More Music soon!"); ?> Link to comment https://forums.phpfreaks.com/topic/224445-simplexml-if-statement/#findComment-1159475 Share on other sites More sharing options...
Maq Posted January 14, 2011 Share Posted January 14, 2011 Glad you got it working but you didn't have any conditions in your original code. Link to comment https://forums.phpfreaks.com/topic/224445-simplexml-if-statement/#findComment-1159479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.