Jump to content

Simplexml if statement?


PeterBrinn

Recommended Posts

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

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!");	  
  
  
  ?>  

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.