1

i have a code, it's getting data but when it gets nothing i want it to return something.

    $upcoming = simplexml_load_file('http://api.website.com'); 

foreach($upcoming->trailer as $x => $updates) {         
$content.= '<center><br><span> ' . $updates->embed . '</span></center>';
}

This is the code. It's getting data but when it got nothing, i would like it to say NO Videos.

How can i do that? I tried strlen() but i couldn't applied it.

I tried strpos but it didn't work either.

4
  • What do you mean by nothing? If simplexml_load_file fails it returns FALSE Commented Apr 3, 2013 at 21:35
  • Yeah when it returns FALSE, it showes nothing. But i want it to say something for example No Videos. Commented Apr 3, 2013 at 21:37
  • 1
    So if ($upcoming === FALSE) $content .= 'no videos'; ? Commented Apr 3, 2013 at 21:38
  • it's not working, by the way it returns <trailers></trailers> Commented Apr 3, 2013 at 22:16

3 Answers 3

1

You can simply check the result of simplexml_load_file and act accordingly

if (false === $upcoming) {
    echo "No Videos";
} else {
    //your foreach loop here
}
Sign up to request clarification or add additional context in comments.

Comments

0
$upcoming = simplexml_load_file('http://api.website.com'); 
if($upcoming == null || $upcoming == false){
    $content = "No videos";
}
else{
    foreach($upcoming->trailer as $x => $updates) {         
    $content.= '<center><br><span> ' . $updates->embed . '</span></center>';
    }
}

Comments

0

Add some flag in your foreach to put a flag if anything has been printed or just check the $upcoming. Then just check the flag and echo "no videos" your message.

Do it like this:

$Something=false;
$upcoming = simplexml_load_file('http://api.website.com'); 

foreach($upcoming->trailer as $x => $updates) {
  if($updates->embed){
    $Something=true;
  }         
  $content.= '<center><br><span> ' . $updates->embed . '</span></center>';
}

if(!$Something){
  echo "<center>No videos</center>";
}

Or:

$upcoming = simplexml_load_file('http://api.website.com');

if(!$upcoming){
  echo "<center>No videos</center>";
}
else{
  foreach($upcoming->trailer as $x => $updates) {
    if($updates->embed){
      $Soemthing=true;
    }         
    $content.= '<center><br><span> ' . $updates->embed . '</span></center>';
  }
}

5 Comments

I am trying this but what if xml sending another text instead of false
i have edited answer, try now. both should work if $upcoming is empty.
if not working from above sample in my answer then try to put print_r($upcoming);die("<br>END"); right after simplexml_load_file and see whats the output.
It still showes up with nothing. By the way i found what xml sends when its empty, it's sending "<trailers></trailers>"
well then edit the php which is doing the output you read @ api.website.com to return nothing or put if($upcoming=="<trailers></trailers>"){echo "No video";}else{//the foreach code}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.