Assign the color of each row according to a row data

0

How can I change the color of some data extracted from my BD, for example if it is on a slope it is yellow and when it is resolved it is green.

  <?php
  $con=@mysqli_connect('localhost', 'root', '', 'bdpagina');
      if(!$con){
    die("imposible conectarse: ".mysqli_error($con));
}
if (@mysqli_connect_errno()) {
    die("Connect failed: ".mysqli_connect_errno()." : ". 
   mysqli_connect_error());
}
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)? 
$_REQUEST['action']:'';
if($action == 'ajax'){
include 'pagination.php'; 
$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))? 
$_REQUEST['page']:1;
$per_page = 10; //la cantidad de registros que desea mostrar
$adjacents  = 4; //brecha entre páginas después de varios adyacentes
$offset = ($page - 1) * $per_page;
//Cuenta el número total de filas de la tabla*/
$count_query   = mysqli_query($con,"SELECT count(*) AS numrows FROM contribuyente ");
if ($row= mysqli_fetch_array($count_query)){$numrows = $row['numrows'];}
$total_pages = ceil($numrows/$per_page);
$reload = 'index.php';

$query = mysqli_query($con,"SELECT * FROM contribuyente  order by id LIMIT $offset,$per_page");

if ($numrows>0){
  ?>
<table class="my-table">
    <thead>
    <tr>
     <th>Folio</th>
      <th>Nombre</th>
      <th>Rut</th>
      <th>Correo</th>
      <th>Telefono</th>
      <th>Direccion</th>
      <th>Poste</th>
      <th>Solicitud</th>
      <th>Fecha</th>
      <th>Estado</th>
      <th>Borrar</th>
      <th>Reporte</th>
    </tr>
  </thead>
  <tbody>
  <?php
  while($row = $res->fetch_array(MYSQLI_BOTH)){
    ?>
    <tr>
      <td><?php echo $row[0];?></td>
      <td><?php echo $row[1];?></td>
      <td><?php echo $row[2];?></td>
      <td><?php echo $row[3];?></td>
      <td><?php echo $row[4];?></td>
      <td><?php echo $row[5];?></td>
      <td><?php echo $row[6];?></td>
      <td><?php echo $row[7];?></td>
      <td><?php echo $row[8];?></td>
      <td><?php echo $row[9];?></td>
      <?php echo "<td><a href='admin.php?id=$row[0]&idborrar=2'><img src='./images/eliminar.png' class='img-rounded'/></a></td>";
       echo "<td><a href='actualizar.php?id=$row[0]'><img src='./images/editar.png' class='img-rounded'></td>";?>

    </tr>
    <?php
  }
  ?>

    
asked by Daniela 28.05.2018 в 23:40
source

2 answers

0

You can do it with bgcolor , a simple example is the following:

<table>
  <tr bgcolor="red">
    <td>Texto</td>
    <td>Texto</td>
    <td>Texto</td>
  </tr>
  <tr bgcolor="yellow">
    <td>Texto</td>
    <td>Texto</td>
    <td>Texto</td>
  </tr>
  <tr bgcolor='#00FF00'>
    <td>Texto</td>
    <td>Texto</td>
    <td>Texto</td>
  </tr>
  <tr bgcolor='#00F'>
    <td>Texto</td>
    <td>Texto</td>
    <td>Texto</td>
  </tr>
</table>

You can also assign hexadecimal values to it as #00FF00 or #00F for other colors you need.

Then for your particular case just vastly place a if php in <tr> to change that color row according to the state value, assuming that 0 is for slope and 1 for resolved the code would be like this:

    <table class="my-table">
    <thead>
    <tr>
     <th>Folio</th>
      <th>Nombre</th>
      <th>Rut</th>
      <th>Correo</th>
      <th>Telefono</th>
      <th>Direccion</th>
      <th>Poste</th>
      <th>Solicitud</th>
      <th>Fecha</th>
      <th>Estado</th>
      <th>Borrar</th>
      <th>Reporte</th>
    </tr>
  </thead>
  <tbody>
  <?php while($row = $res->fetch_array(MYSQLI_BOTH)){
    ?>
<!-- Justo aqui debajo -->
    <tr bgcolor="<?php if($row[8]==1) echo 'green';else echo 'yellow' ?>" >
      <td><?php echo $row[0];?></td>
      <td><?php echo $row[1];?></td>
      <td><?php echo $row[2];?></td>
      <td><?php echo $row[3];?></td>
      <td><?php echo $row[4];?></td>
      <td><?php echo $row[5];?></td>
      <td><?php echo $row[6];?></td>
      <td><?php echo $row[7];?></td>
      <td><?php echo $row[8];?></td>
      <td><?php echo $row[9];?></td>
      <?php echo "<td><a href='admin.php?id=$row[0]&idborrar=2'><img src='./images/eliminar.png' class='img-rounded'/></a></td>";
       echo "<td><a href='actualizar.php?id=$row[0]'><img src='./images/editar.png' class='img-rounded'></td>";?>

    </tr>
    <?php  }  ?>
</table>

I hope it helps you. Greetings.

    
answered by 28.05.2018 в 23:54
0

The easiest way, to avoid having to check the value you are going to use to assign the color to the row, every time you print the row, is to use that value as a CSS class for that row and add that class to your styles with the color you want as a background. Otherwise, if you had several colors according to different values, the code would become complicated.


I have simplified the code for the example, where only "name" and "state" are printed for each row. The rows where the "state" is "Resolved" are printed with a green background, so that we get a table like the following:

This is the PHP code:

<?php
/**
 * https://es.stackoverflow.com/questions/168598
 *
 * Cambiar el color de las filas de una tabla según un valor
 *
 */

    $rows = [
        [
            'name'   => 'Foo',
            'status' => 'Resuelto',
        ],
        [
            'name'   => 'Baz',
            'status' => 'Resuelto',
        ],
        [
            'name'   => 'Bart',
            'status' => 'Pendiente',
        ],
        [
            'name'   => 'John Doe',
            'status' => 'Resuelto',
        ],
        [
            'name'   => 'Fake',
            'status' => 'Pendiente',
        ],
    ];

?>

<style type="text/css">
    table {
        border-collapse: collapse;
        border: 1px solid grey;
    }
    th {
        background: lightgrey;
        font-weight: bold;
    }
    th, td {
        min-width: 200px;
        padding  : 9px;
        text-align: center;
    }
    .resuelto {
        background: #1fe039; /* green for solved status */
    }
</style>

<table class="my-table">
    <thead>
        <tr>
            <th>Nombre</th>
            <th>Estado</th>
        </tr>
    </thead>
    <tbody>

<?php

    $row_position = 0;
    $rows_number  = count($rows);

    // each iteration prints a file
    while ($row_position < $rows_number) {

        echo '<tr class="' . $rows[$row_position]['status'] . '">';

        echo '<td>' . $rows[$row_position]['name']   . '</td>';
        echo '<td>' . $rows[$row_position]['status'] . '</td>';

        echo '</tr>';

        $row_position++;
    }

?>

    </tbody>
</table>


  

Edited

Now, to assign other background colors for the rows according to the "state", you only have to add in your styles the corresponding classes to those states, for example to print with a yellow background the rows with "pending" status add the class:

.pendiente {
    background: yellow:
}
    
answered by 04.01.2019 в 12:24