html - Conditional CSS by PHP
Could anybody tell me what I am going wrong?
I am trying to change CSS style asking with PHP.
<?php
include 'connect.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="refresh" content="10">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<title>Lista de mensajes</title>
</head>
<body>
<div class="container-fluid mb-10">
<table class="table table-striped fs-2">
<thead class="text-uppercase">
<tr>
<th scope="col" class="col-sm-2">fecha</th>
<th scope="col">mensaje</th>
<th scope="col" class="col-sm-1">prioridad</th>
</tr>
</thead>
<tbody>
<?php
$sql="SELECT * FROM mensajes WHERE 1 ORDER BY prioridad DESC,fecha DESC";
$resultado=mysqli_query($con,$sql);
while($row=mysqli_fetch_assoc($resultado)){
$id=$row['id'];
$hotel=$row['hotel'];
$mensaje=$row['mensaje'];
$fecha=new datetime($row['fecha']);
$fecha=$fecha->format('d-m-y H:i:s');
$prioridad=$row['prioridad'];
if($prioridad==true){
echo '<tr class="text-danger">';
}else{
echo '<tr>';
}
echo '<td scope="row">'.$fecha.'</td>
<td>'.$hotel." - ".$mensaje.'</td>
<td>'.$prioridad.'</td>
</tr>';
}
?>
</tbody>
</table>
<?php
if(!mysqli_close($con)){
die(mysqli_error());
}
?>
</div>
</body>
</html>
HTML output seems to be correct but I can only see the last entry in red
<table class="table table-striped fs-2">
<thead class="text-uppercase">
<tr>
<th scope="col" class="col-sm-2">fecha</th>
<th scope="col">mensaje</th>
<th scope="col" class="col-sm-1">prioridad</th>
</tr>
</thead>
<tbody>
<tr class="text-danger"><td scope="row">23-10-22 10:58:54</td>
<td>PLB - rojo OOOO?</td>
<td>1</td>
</tr><tr class="text-danger"><td scope="row">23-10-22 08:58:02</td>
<td>BPC - BPC y prioridad</td>
<td>1</td>
</tr><tr><td scope="row">24-10-22 08:01:54</td>
<td>BPK - TEST</td>
<td>0</td>
</tr><tr><td scope="row">23-10-22 10:26:29</td>
<td>BGA - Prueba BGA con prioridad</td>
<td>0</td>
</tr>
</tbody>
</table>
connect.php just make a connection to a mysql database with a single table.
I would like to show the text in red when the priority is true or 1
Thanks in advance
Answer
Solution:
Firstly, don??�t use while
for database fetch. Use instead if
Secondly, something like $avar=areturn()
will always be true, so you should define $row
separately before the loop.