Khailz
Khailz

Reputation: 89

How make my PHP code print to seperate divs?

I am trying to but the prints from this php code into seperate html div's so I can customize them with css. I got a css file made and it shows but everything is printed out into one div called body class=" hasGoogleVoiceExt".

<?php
class Bot {
    public $botHost = '127.0.0.1';
    public $botPort = 8087;
    public $botId = NULL;
    public $token = NULL;
    public function __construct($botHost = '127.0.0.1', $botPort = 8087, $botId = NULL) {
        $this->botHost = $botHost;
        $this->botPort = $botPort;
        if ($botId == NULL) {
            $botId = $this->DefaultBot();
        }
        $this->botId = $botId;
    }
    public function DefaultBot() {
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/botId',
            CURLOPT_RETURNTRANSFER => 1
        ));
        $data = curl_exec($ch);
        curl_close($ch);
        $json = json_decode($data, TRUE);
        return $json['defaultBotId'];
    }
    public function Login($username, $password) {
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/bot/login',
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_POSTFIELDS => json_encode(array('username' => $username, 'password' => $password, 'botId' => $this->botId)),
            CURLOPT_HTTPHEADER => array('Content-Type: application/json')
        ));
        $data = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($code != 200) return NULL;
        $this->token = json_decode($data, TRUE)['token'];
    }
    public function GetInstances() {
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/bot/instances',
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_HTTPHEADER => array('Authorization: bearer '.$this->token)
        ));
        $data = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($code != 200) return NULL;
        $json = json_decode($data, TRUE);
        return $json;
    }
    public function GetInstanceStatus($instanceId) {
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/bot/i/'.$instanceId.'/status',
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_HTTPHEADER => array('Authorization: bearer '.$this->token)
        ));
        $data = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($code != 200) return NULL;
        $json = json_decode($data, TRUE);
        return $json;
    }
}

$bot = new Bot('127.0.0.1', 8087);
$bot->Login('admin', 'foobar');
$instances = $bot->GetInstances();
for ($i = 0; $i < count($instances); $i++) {
    $info = $bot->GetInstanceStatus($instances[$i]['uuid']);
    if ($info['currentTrack'] != NULL && $info['playing']) {
        printf("%s is playing %s by %s\n", $instances[$i]['nick'], $info['currentTrack']['title'], $info['currentTrack']['artist']);
    } else {
        printf("%s is not playing anything right now\n", $instances[$i]['nick']);
    }
    echo '<link href="botcss/styles.css" rel="stylesheet" type="text/css" />';
}

I'm currently testing it out here http://theunlighted.com/nowplaying.php

Upvotes: 1

Views: 102

Answers (1)

Rob W
Rob W

Reputation: 9142

First things first: Your <link [...]> needs to be output before the for() loop.

Secondly, to output divs in a way (that I think you're meaning to do) is simple:

for($i = 0; $i < 123; $i++) {
  echo '<div class="foo foo_'.$i.'">';
  // do other output here.
  echo '</div>';  
}

Upvotes: 1

Related Questions