How to change the color of the table row (php) according to the value?

-2

<?php 
include_once("func/functions.php"); // Chama o arquivo de funções

function diaria($codmot,$periodo)
{
	$data =  (explode(" - ",$periodo));
	$datIni =  date('Y-m-d',strtotime($data[0]));
	$datFin =  date('Y-m-d',strtotime($data[1]));

	$conn_datapar = connect();
	$sql = "SP_TI_DIARIAS_E_LANCHES $codmot,'$datIni 00:00:00', '$datFin 23:59:59', 117";
	$stmt = sqlsrv_query($conn_datapar, $sql);	

	while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
	{
		$ficha = $row["CODFIC"];
		$data = $row["DATREF"]->format('d-m-Y H:i');
		$obs = $row["OBSERV"];
		$valor = $row["VLRADI"];

		echo "
			<tbody>
				<tr>
					<td>".$ficha."</td>
					<td>".$data."</td>
					<td>".$obs."</td>
					<td>R$".$valor."</td>
				</tr>
			</tbody>
		";
	}
}

Good afternoon guys! I really need your help. I have a table (php), filled with information from DB, I need these results, when the value of. $ Value., Is negative, the line is red, when positive it turns green. NOTE: I used the grid, and it worked, it was exactly as I wanted it, but the table gets better visualization and my boss wants it done with a table. Does anyone know how to help me? Thanks in advance.

    
asked by anonymous 22.05.2018 / 21:18

1 answer

1

Within the while make a if else to set the color depending on the value of the $valor variable and place it on the table line <tr style='background-color:".$color."'>

$valor = $row["VLRADI"];
// decide a cor do fundo das linhas
if($valor>0){
  $color="green";
}else{
  $color="red";
}

    echo "
        <tbody>
            <tr style='background-color:".$color."'>
                <td>".$ficha."</td>
                <td>".$data."</td>
                <td>".$obs."</td>
                <td>R$".$valor."</td>
            </tr>
        </tbody>
    ";
  

Nothing prevents you from using classes with CSS

CSS

  .verde{
     background-color:green;
  }

  .vermelha{
     background-color:red;
  }

PHP

$valor = $row["VLRADI"];

if($valor>0){
  $class="verde";
}else{
  $class="vermelha";
}

    echo "
        <tbody>
            <tr class='".$class."'>
                <td>".$ficha."</td>
                <td>".$data."</td>
                <td>".$obs."</td>
                <td>R$".$valor."</td>
            </tr>
        </tbody>
    ";
    
22.05.2018 / 22:33