简体   繁体   中英

Color table row by status

I wanna do if in by my status field. if my status is "blocked" so the font background color will be in red and if the status is "authorized" so in green.

how should i do that?

    foreach($data as $row)
   {
    $output .= '
    <tr>

     <td>'.$row->client_name.'</td>
     <td>'.$row->adrress.'</td>
     <td>'.$row->occupation.'</td>
     <td>'.$row->payeee.'</td> 
     <td>'.$row->status.'</td> 


    </tr>
    ';
   }

Try this

foreach($data as $row)
{
$output .= '
<tr>

 <td>'.$row->client_name.'</td>
 <td>'.$row->adrress.'</td>
 <td>'.$row->occupation.'</td>
 <td>'.$row->payeee.'</td> 
   @if($row->status == "blocked")
   <td> <button type="button" class="btn btn-danger">blocked</button> </td>
   @elseif($row->status == "authorized")
   <td> <button type="button" class="btn btn-success">authorized</button> </td>
   @endif
 </tr>

OR

foreach($data as $row){
    $output .= '
    <tr>

     <td>'.$row->client_name.'</td>
     <td>'.$row->adrress.'</td>
     <td>'.$row->occupation.'</td>
     <td>'.$row->payeee.'</td>';
     if($row->status == "bad"){
        $output .= '<td style="background-color:red;">'.$row->status.'</td>';
     } else if($row->status == "good") {
       $output .= '<td style="background-color:green;">'.$row->status.'</td>';
    }
    $output .= '</tr>';
}

You shouldn't really be dealing with view logic in your controller.

But to do what you want in the controller

foreach($data as $row){
    $output .= '
    <tr>

     <td>'.$row->client_name.'</td>
     <td>'.$row->adrress.'</td>
     <td>'.$row->occupation.'</td>
     <td>'.$row->payeee.'</td>';
     if($row->status == "bad"){
        $output .= '<td style="background-color:red;">'.$row->status.'</td>';
     } else if($row->status == "good") {
       $output .= '<td style="background-color:green;">'.$row->status.'</td>';
    }
    $output .= '</tr>';
}

However what you should do is handle it in your blade file. Also you probably want to create styles in your style sheet and apply classes rather than inline the css.

this is the code that worked

foreach($data as $row){
$output .= '
<tr>

 <td>'.$row->client_name.'</td>
 <td>'.$row->adrress.'</td>
 <td>'.$row->occupation.'</td>
 <td>'.$row->payeee.'</td>';
 if($row->status == "bad"){
    $output .= '<td style="background-color:red;">'.$row->status.'</td>';
 } else if($row->status == "good") {
   $output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM