Я использую запросы mysql для извлечения данных из базы данных. Все мои данные хорошо отображаются в таблицах. Теперь я хочу раскрасить статус по значению меньше 2 или больше 3. Приведенный ниже код не работает. Нужна помощь.
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) > 0)
{
$output .= '<div class = "table-responsive">
<table class = "table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
?>
<?php
function status_style($row) {
if ($row < 2) return 'background-color: #ff0000'; //red
if ($row > 3) return 'background-color: #33cc33'; //green
return '';
}
?>
<?php
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
?>






Вы можете попробовать что-то вроде этого:
<?php
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) > 0)
{
$output .= '<div class = "table-responsive">
<table class = "table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
function status_style($row) {
if ($row < 2) return $color = '#ff0000'; //red
if ($row > 3) return $color = '#33cc33'; //green
return $color = '';
}
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr style='"'background-color: '"' . $color . '"'>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
Вы ничего не сделали с цветом фона.
Вы уже создали функцию для раскрашивания строк, но не вызывали эту функцию во время отображения вывода. Вот почему это не работает.
Измените переменную $output на:
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td style = "'.status_style($row["status"]).'">'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';