Show colors taken from a php database

2

I have a web application, which has a form for registering new users; Each user is assigned a unique color, taken from BD (MySQL) .

I have a users table and a table colors, having two registered users, I want to show the color assigned to each user when displaying the user table; So far I have managed to show the color hexadecimal code, which is the one stored in BD .

Does anyone know how to read that code and show the color in the cell?

<?php
    $tusers = $conexion->query("SELECT * FROM user u LEFT JOIN cat_niveles n ON u.Nivel=n.id LEFT JOIN cat_centros c ON u.ID_CC=c.id LEFT JOIN cat_colores k ON u.id_color=k.id");
    $listU = $tusers->fetch_all(MYSQLI_ASSOC);
    for ($i=0; $i < count($listU); $i++) { 
        echo '<tr>';    
        echo "<td>".$listU[$i]['Nombre']."</td>".
            "<td>".$listU[$i]['User']."</td>".
            "<td>".$listU[$i]['nombre_nivel']."</td>".
            "<td>".$listU[$i]['Nombre_CC']."</td>".
            "<td>".$listU[$i]['Fecha_Registro']."</td>".
            "<td>".$listU[$i]['has_color']."</td>";
    }
?>
    
asked by cgasparico 21.12.2018 в 18:38
source

2 answers

1

Something like that should work. You just have to put the color on a style tag, with the background-color property.

<?php
    $tusers = $conexion->query("SELECT * FROM user u LEFT JOIN cat_niveles n ON u.Nivel=n.id LEFT JOIN cat_centros c ON u.ID_CC=c.id LEFT JOIN cat_colores k ON u.id_color=k.id");
    $listU = $tusers->fetch_all(MYSQLI_ASSOC);
    for ($i=0; $i < count($listU); $i++) { 
        echo '<tr>';    
        echo "<td>".$listU[$i]['Nombre']."</td>".
            "<td>".$listU[$i]['User']."</td>".
            "<td>".$listU[$i]['nombre_nivel']."</td>".
            "<td>".$listU[$i]['Nombre_CC']."</td>".
            "<td>".$listU[$i]['Fecha_Registro']."</td>".
            "<td style='background-color: #.$listU[$i]['has_color']. ;'>""</td>";  
    }
?>
    
answered by 21.12.2018 / 18:59
source
1

I guess you want something like this:

<?php
    $tusers = $conexion->query("SELECT * FROM user u LEFT JOIN cat_niveles n ON u.Nivel=n.id LEFT JOIN cat_centros c ON u.ID_CC=c.id LEFT JOIN cat_colores k ON u.id_color=k.id");
    $listU = $tusers->fetch_all(MYSQLI_ASSOC);
    for ($i=0; $i < count($listU); $i++) { 
        echo '<tr>';    
        echo "<td>".$listU[$i]['Nombre']."</td>".
            "<td>".$listU[$i]['User']."</td>".
            "<td>".$listU[$i]['nombre_nivel']."</td>".
            "<td>".$listU[$i]['Nombre_CC']."</td>".
            "<td>".$listU[$i]['Fecha_Registro']."</td>".
            "<td><div style='width:20px;height:20px;background-color:".$listU[$i]['has_color']."'></div></td>";
    }
?>

If the code does not contain the # you just have to add it otherwise leave it like this.

    
answered by 21.12.2018 в 18:54