Status of buttons with Codeigniter

0

I would like to know how in Codeigniter to change the state of the activate button that is green, that when it is deactivated it is red, but when I give it to activate this green one.

Controller code:

if ($activos =='true'){
    $actinomtest ='Activar';
    $valoresacti ='1';
    $colorbtn = 'class="btn btn success';
}else{
    $actinomtest ='Desactivar';
    $valoresacti ='0';
}

And in this line within the same Controller called those variables:

foreach ($data as $row){
        $botones='<button class="btn btn-primary btn-sm btnedit text-center" onclick="botontest('.$row->idTest.')">Editar</button>
                  <a class="btn btn-warning" href="#">Editar tabla</a>
                  <button class="btn btn-danger btn-sm btnactest text-center" onclick="btnactivtest('.$row->idTest.', '. $valoresacti.', '.$colorbtn .')">'.$actinomtest.'</button>';
}
    
asked by Pag 03.01.2019 в 15:31
source

1 answer

2

There are several ways to do it. The simplest, for me, would be to save in the variable $colorbtn only the class that gives the color to your button. In your case they would be the class btn-success for the green color and btn-danger for the red one.

Your code could be as follows:

if ($activos =='true'){

  // Guardamos la clase que da el color verde
  $colorbtn = 'btn-success';

  $actinomtest ='Activar';
  $valoresacti ='1';

}else{

  // Guardamos la clase que da el color rojo
  $colorbtn = 'btn-danger';

  $actinomtest ='Desactivar';
  $valoresacti ='0';      
}

Now concatenate the variable $colorbtn within the class attribute of the button like this: <button class="btn '.$colorbtn.' btn-sm btnactest text-center"' ...

The complete code would look like this:

 foreach ($data as $row){
    $botones='<button class="btn btn-primary btn-sm btnedit text-center" onclick="botontest('.$row->idTest.')">Editar</button>
              <a class="btn btn-warning" href="#">Editar tabla</a>
              <button class="btn '.$colorbtn.' btn-sm btnactest text-center" onclick="btnactivtest('.$row->idTest.', '. $valoresacti.', '.$colorbtn .')">'.$actinomtest.'</button>';

}

Here is a link to the bootstrap 4 documentation on the buttons: Bootstrap buttons

    
answered by 05.01.2019 / 20:25
source