problem with style change using php and change of values in echo

1

I'm trying to create a style to show it if the field is 1 or 0

When it's 1 and 0 it shows me well, the thing is that if I'm wrong and I want it     appear the red style the green still appears.

<?php if($resultado['id_usuario_autoriza']): ?>
            <tr>

              <?php if($resultado['autorizado']): ?>
                <th style="color:green;">Autorizado Por:</th>
                <td style="color:green;"><?=$resultado_usuario_autoriza['personaNombre']?> <?=$resultado_usuario_autoriza['apellido']?><?php if ($resultado_usuario_autoriza['id_departamento']= 1) $departamento1 = "Sistemas"; ?>  <?php  echo $departamento1; ?></td>
              <?php elseif(!$resultado['autorizado']): ?>
                <th style="color:red;">No Autorizado Por:</th>
                <td style="color:red;"><?=$resultado_usuario_autoriza['personaNombre']?> <?=$resultado_usuario_autoriza['apellido']?></td>
              <?php endif; ?>

            </tr>
          <?php else: ?>
            <tr>

              <th>Autorizado Por:</th>
              <td>Sin Autorizar</td>

            </tr>
      <?php endif; ?>

Here in the last image I show what I set when authorizing or un-authorizing the request.

    
asked by Juan Ortiz 29.10.2018 в 15:30
source

2 answers

0

The field autorizado must be NULL and then make the change: 1 for autorizado and 2 for pendiente

if($resultado['autorizado'] == 1){
   echo "Autorizado";
} elseif($resultado['autorizado'] == 2) {
   echo "Pendiente";
}else {
   echo "No autorizado";
}
    
answered by 29.10.2018 в 16:01
0

Juan looks at this very simple example. I think that is what you need. If so, you tell me and I also indicate a more elegant way to build your elements in the PHP code.

It is recommended to use CSS classes instead of putting style rules directly in the elements. Why? For many reasons. Let's just put one: imagine a page with thousands of files where the styles (colors, sizes, margins ...) are directly in the elements and there is a need to change something. It will be necessary to open the files and modify those attributes again in situ. With CSS rules you change the value only in the CSS file and the style is applied to all the elements that have that class.

This rule applies here and you'll see how easy it is to change the color from green to red or vice versa.

I hope it serves you.

/*Asignamos un listener a todos los elementos con la clase "button"*/
document.querySelectorAll(".button").forEach(function(elem) {
  elem.addEventListener('click', setColor, false);
});

function setColor() {
  /*Referencia (por su id) a la fila que cambiará de color*/
  var trAutorizado = document.querySelector("#trf");
  /*Usamos el valor de name de cada botón para setear la clase CSS*/
  trAutorizado.className = this.name;
}
.btn-autorizar {
  float: right;
  text-align: right;
}

.red {
  color: red;
}

.green {
  color: green;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css" rel="stylesheet" />

<div class="btn-autorizar">
  <a class="button is-success" name="green">Autorizar</a>
  <a class="button is-danger" name="red">No Autorizar</a>
</div>
<table class="table">
  <tbody>
    <tr>
      <td>Nombre: </td>
      <td>Juan Ortiz</td>
    </tr>
    <tr>
      <td>Tipo de solicitud:</td>
      <td>Soporte</td>
    </tr>
    <tr class="green" id="trf">
      <td>Autorizado por: </td>
      <td>Juan Ortiz</td>
    </tr>
  </tbody>

</table>
    
answered by 30.10.2018 в 00:14