在foreach循环中,我如何在echo中为文本着色?


How would I color text inside echo while in a foreach loop?

我是这门语言的新手,我的问题如下。所以,我有这段代码…

<?php
echo "<table style='border: solid 1px black;'>";
echo "<tr><th><font color='green'><center>Username</th><th><font color='green'><center>Total EXP</th><th><font color='green'><center>Online Time</th></tr>";
class TableRows extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }
    function current() {
        return "<td style='width:100px;border:1px solid black;'>" . parent::current(). "</td>";
    }
    function beginChildren() {
        echo "<tr>";
    }
    function endChildren() {
        echo "</tr>" . "'n";
    }
}
$servername = "localhost";
$username = "root";
$password = "123";
$dbname = "hiscores";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT username, overall_xp, onlinetime FROM hs_users ORDER BY overall_xp DESC LIMIT 0, 10");
    $stmt->execute();
    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
         echo "$v";
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?> 

我想居中,并使结果白色。我已经用了很多种方法,但我还是不知道该怎么做。有人能帮我一下吗?我试过在这个部分添加字体标签和东西

回声"v"美元;

。结果总是这样

http://prntscr.com/7q50yi

似乎有些外部文件正在改变CSS。您可以尝试!important与每个样式,如:

echo "<div style='color:#ffffff !important;text-align:center !important">$v</div>";

使用<div>,如果您在下一行中使用每一行,则可以使用<span>