如何使我的PHP代码打印到单独的div?

时间:2015-03-20 00:24:27

标签: php html css

我正在尝试将这个PHP代码中的打印件分成单独的html div,以便我可以使用css自定义它们。我得到了一个css文件,它显示但是所有内容都被打印成一个名为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" />';
}

我目前正在这里测试http://theunlighted.com/nowplaying.php

1 个答案:

答案 0 :(得分:1)

首先要做的是:<link [...]>需要在for()循环之前输出。

其次,以某种方式输出div(我认为你有意义去做)很简单:

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