简体   繁体   中英

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

I'm new to this language, and my question is as follows. So, I have this piece of code...

<?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>";
?> 

I want to center, and make the result white colored. I've tried it multiple ways, I still can't figure out how to do it.. Could someone help me, please? I've tried adding font tags and stuff to this part

echo "$v";

Nothing. The outcome is always like this

http://prntscr.com/7q50yi

Seems like may some external file is changing the CSS.You can try !important with each style like:

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

use <div> if you eant each row in next line else you can use <span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM