I have this snippet below, I'm trying to query if a cast is online or not (That part works when I take it outside the while loop) but then in the while loop run through until I find an online cast and then keep it there.

If I remove the break; in the ($stream == null) it will run the array but only for my first $url (So, the embed will run 4 times but only query the first $url).

So, if the stream is online. It will show all the streams in the array despite them being offline/online.

How can I make it so that it queries the array and executes the first embed of an online stream?

<?php
    //On/Off
    $url = "https://api.twitch.tv/kraken/streams/greatbritishbg";
    $json = file_get_contents($url);
    $json = json_decode($json);
    $stream =  $json->stream;

        $streamers = array("greatbritishbg", "Gecko194", "jayw89","DethridgeCraft","reninsane"); 
        while (list(, $val) = each($streamers)) {

                $url = "https://api.twitch.tv/kraken/streams/".$val;
            //var_dump($json);
            $url1 = "https://api.twitch.tv/kraken/channels/". $val;
            $json1 = file_get_contents($url1);
            $json1 = json_decode($json1);
            $logo =  $json->preview->medium;
            $game = $json1->channels->game;

            if($stream != null){                    
               echo'<header class="intro">';
               echo'<div class="intro-body">';
               echo'<div class="container">';
               echo'<div class="row">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<h1 class="brand-heading">GBBG</h1>';
               echo'<p class="intro-text"></p>';
               echo "Live";
               echo '<iframe src="http://www.twitch.tv/'. $val .'/embed" frameborder="0" scrolling="no" height="378" width="620"></iframe>';  

            }
            if($stream == null){
               echo'<header class="intro">';
               echo'<div class="intro-body">';
               echo'<div class="container">';
               echo'<div class="row">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<h1 class="brand-heading">GBBG</h1>';

               echo'<p class="intro-text">We are all offline at the moment.</p>';
               echo'<p class="intro-text">When we are online, this page will change</p>';
               break;
        }
        echo $val;
    }
?>

How can I make it so that it queries the array and executes the first embed of an online stream?

Add a break; to exit your while loop.

Hey Prit,

I've added the break into the while loop instead of the if statements. It runs through array hits the first and then defines if it's online or offline.

    $streamers = array("greatbritishbg", "Gecko194", "jayw89","DethridgeCraft","reninsane");

If I make the first array "DeathridgeCraft" it will go live. So he is live at the moment but because it's after an offline channel it won't relay through the array.

Here is my full script:

<?php
        $streamers = array("greatbritishbg", "Gecko194", "jayw89","DethridgeCraft","reninsane"); 
        while (list(, $val) = each($streamers)) {

            $url = "https://api.twitch.tv/kraken/streams/".$val;
            $json = file_get_contents($url);
            $json = json_decode($json);
            $stream =  $json->stream;


            //var_dump($json);
            $url1 = "https://api.twitch.tv/kraken/channels/". $val;
            $json1 = file_get_contents($url1);
            $json1 = json_decode($json1);
            $logo =  $json->preview->medium;
            $game = $json1->channels->game;

            if($stream != null){                    
               echo'<header class="intro">';
               echo'<div class="intro-body">';
               echo'<div class="container">';
               echo'<div class="row">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<h1 class="brand-heading">GBBG</h1>';
               echo'<p class="intro-text"></p>';
               echo "Live";
               echo '<iframe src="http://www.twitch.tv/'. $val .'/embed" frameborder="0" scrolling="no" height="378" width="620"></iframe>';

            }
            if($stream == null){
               echo'<header class="intro">';
               echo'<div class="intro-body">';
               echo'<div class="container">';
               echo'<div class="row">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<h1 class="brand-heading">GBBG</h1>';

               echo'<p class="intro-text">We are all offline at the moment.</p>';
               echo'<p class="intro-text">When we are online, this page will change</p>';

        }
        break;
        echo $val;
    }
?>

Oh wait, I misunderstood. You want to show all streams but embed only one online one.

Remove the break. Add a boolean before the while $embedded = false; Around line 28 put:

if (!$embedded)
{
    $embedded = true;
    // Line 28 here
}

Thanks very much prit :) Rather simple solution, I can't believe I didn't think of it ^>^

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.