Home Have a text from database to get a color out from what it says
Reply: 3

Have a text from database to get a color out from what it says

shittycoder Published in 2017-12-07 23:18:02Z

Hey guys, im not good with coding so after failing to find out how from googling Im gonna try get help from the pros.

So, im just making something for fun cause im bored. I have a sql database with a table that consists of name, link and status. The status has 3 options, Permanently Banned, Temporarily Banned and Kicked.

Currently I have it like this putting font color in the code, but then it all get in red text.

while($row = $result->fetch_assoc()) {
    echo "<tr>
    <td class='text-left'> ". $row["name"]. " </td>
    <td class='text-left'> ". $row["link"]. "</td>
    <td class='text-left'><b><font color='red'> " . $row["status"] . "</font></b></td></tr>";


I want Permanently Banned to be in red, Temporarily Banned in orange and Kicked in green. I don't understand how to make the colors appear like that.

I appreciate any help, im still a newbie when it comes to coding so im sorry if this is a really easy solution that I failed to find :P

I also have a problem with the link, but im just gonna let it be for now, maybe I'l ask after this is fixed..

Thanks! :)

Alessio Reply to 2017-12-07 23:29:36Z

You can simply add a CSS class for each case and then style it via CSS. Of course you have to clean the name in order to remove spaces with underscore or hyphens.

while($row = $result->fetch_assoc()) {
    $cleanStatus = strtolower(preg_replace('/\s+/', '_', $row["status"]));
    echo "<tr>
    <td class='text-left'> ". $row["name"]. " </td>
    <td class='text-left'> ". $row["link"]. "</td>
    <td class='text-left'><b class='".$cleanStatus."'> " . $row["status"] . "</b></td></tr>";

So your CSS could look like:

.permanently_banned {
   color: red;

.temporarily_banned {
   color: orange;

.kicked {
   color: green;
RiggsFolly Reply to 2017-12-07 23:29:06Z

Use your status as a CSS class:

$statusclass = str_replace(' ', '-', strtolower($row[status]));`

then change your HTML for the status:

`<td class='text-left " . $statusclass . "'></td></tr>";`

Then add CSS to your output, preferably in the <head>, but anywhere might do:

<style type="text/css">
.permanently-banned { font-weight: bold; color: Red; }
.temporarily-banned { font-weight: bold; color: Orange; }
.kicked { font-weight: bold; color: Green; }
Chris Schaller
Chris Schaller Reply to 2017-12-08 01:20:45Z

A quick solution that only affects the short example of code you provided might be to use an inline if

while($row = $result->fetch_assoc()) {
    echo "<tr>
    <td class='text-left'> ". $row["name"]. " </td>
    <td class='text-left'> ". $row["link"]. "</td>
    <td class='text-left'><b><font color='" . ($row["status"]=="Permanently Banned" ? "red" : ($row["status"]=="Temporarily Banned" ? "orange" : "green")) . "'> " . $row["status"] . "</font></b></td></tr>";

You could also do the conditional logic very easily in SQL, for new developers I often find this is an easier approach because it can be easier to manipulate the sql query than it might be to mess with the rendering code. You didn't provide your query, so I will assume what it is, (in future posts, please provide the sql query so we can provide a more complete solution) hopefully you can follow the switch logic and apply that to your own code:

SELECT name, link, status,
    CASE status 
        WHEN 'Permanently Banned' THEN 'Red' 
        WHEN 'Temporarily Banned' THEN 'Orange' 
        ELSE 'Green' 
    END as color 
FROM MyStatusTable

Then your rendering code is cleaner:

while($row = $result->fetch_assoc()) {
    echo "<tr>
    <td class='text-left'> ". $row["name"]. " </td>
    <td class='text-left'> ". $row["link"]. "</td>
    <td class='text-left'><b><font color='" . $row["color"] . "'> " . $row["status"] . "</font></b></td></tr>";

Happy coding :)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.320586 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO