0

I want to print a grid of buttons in which the buttons gonna have specific color according to condition and i am not able to escape php in html attribute

<?php

$i=0;
$table = '<table class="table">';
foreach($rooms as $value)
{
    $s = $value['Status'];
    if ($i % 5 == 0) 
    { 
      $table .= '<tr><td><button class="<?=($s==0)? btn btn-primary : btn btn-danger?>">'. $value["RoomNo"] .'</button></td>';
  }   
  else 
  {
      $table .= '<td><button class="<?php echo ($s==0)?btn btn-primary : btn btn-danger?>">'. $value["RoomNo"] .'</button></td>';
  }
  $i++;
}
$table .= '</tr></table>';
?>
<?php  echo $table; ?>

expected result should be like buttons should have btn primary class whose status is 0 and for the rest red

brombeer
  • 8,716
  • 5
  • 21
  • 27

1 Answers1

0

Try this

<?php

   $i=0;
   $table = '<table class="table">';
   foreach($rooms as $value)
   {
       $btn_class = $s==0 ? "btn btn-primary" : "btn btn-danger";
       $s = $value['Status'];
       if ($i % 5 == 0) 
       { 
          $table .= '<tr><td><button class="'.$btn_class.'">'. $value["RoomNo"] .'</button></td>';
       }   
       else 
       {
          $table .= '<td><button class="'.$btn_class.'">'. $value["RoomNo"] .'</button></td>';
       }
       $i++;
   }
   $table .= '</tr></table>';
   echo $table; ?>
Danish Ali
  • 2,354
  • 3
  • 15
  • 26