If online show this, else show this (not working)

There’s a problem there. the “Viewers” are not stored in the channel data, but in the stream data.
Instead of saving the $chdata[‘channel’], it would be best to just save the $chdata instead:

    $live = array();
$channels = array("themittanidotcom", "djyumene", "mym_alkapone", "Rightrevgoldstein", "daopa", "streamerhouse", "cabochardlol", "moanygamer", "bajheera", "lelaaone", "Old_Bearded_Gamer");

// This part does stuff ... and things! No touchie!
$streamData = json_decode(@file_get_contents("https://api.twitch.tv/kraken/streams?channel=".implode(",",$channels)), true);

foreach ($channels as $name) { 
    $live[$name] = null; 
}

foreach ($streamData["streams"] as $stream)  {
    $live[$chdata["channel"]["name"]] = $stream;        
}
    foreach ($live as $name => $stream) {
                if($stream != null){
                        $game =         $stream['channel']['game'];
                        $logo =         $stream['channel']['logo'];
                        $viewers =         $stream['viewers'];
                echo "<p><img src='$logo' height='45' width='45' style='float:left;'><a href='http://twitch.tv/$name/embed' target='tbox'><font style='font-size: 1rem;line-height: 1.4;white-space: nowrap;font-weight: 700;color: #B9A3E3;text-decoration: none;font-family: 'Open Sans',sans-serif;'>$name</font></a><br><font style='font-size: 0.75rem;line-height: 1;white-space: nowrap;font-weight: 700;color: #3366FF;text-decoration: none;font-family: 'Open Sans',sans-serif;'>Playing: <img src='online.png' height='7' width='7'> $game<br>Viewers: $viewers</font></p>";
                } else{
                echo "<p><img src='$logo' height='45' width='45' style='float:left;'><a href='http://twitch.tv/$name/embed' target='tbox'><font style='font-size: 1rem;line-height: 1.4;white-space: nowrap;font-weight: 700;color: #B9A3E3;text-decoration: none;font-family: 'Open Sans',sans-serif;'>$name</font></a><br><font style='font-size: 0.75rem;line-height: 1;white-space: nowrap;font-weight: 700;color: #3366FF;text-decoration: none;font-family: 'Open Sans',sans-serif;'>Playing: <img src='offline.png' height='7' width='7'> Offline<br><br></font></p>";
                    }
    }

In your old code, you got the viewers of the last user every time.
as you can see in the last forloop, you need the entire stream object, not just the channel object.

DO note:
I renamed every $chdata to $stream, to make the variable more clear

Edit: for every offline channel, if you would want toget their logo use:
https://api.twitch.tv/kraken/channels/test_channel

You will have to use kraken/channels for every offline channel to get their logo, there is no faster way to do this.

1 Like