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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.