Dynamically change text color based on a value retrived from database


I am working with PHP/Laravel. I query my TaskType model to retrieve specific key & values. I then proceed to assign a color based on the retrieved value. For example, if the value is START, i assign this value ‘colorStart’, which is just a variable that retrieves the associated color of my ‘START’ value.

public function index()
    {
        $eventStartData = TaskType::all()->where('event', 'START');
        $eventStopData = TaskType::all()->where('event', 'STOP');
        $eventReportData = TaskType::all()->where('event', 'REPORT');
        $colorStart = TaskType::all()->where('colors', '#4f5705');
        $colorStop = TaskType::all()->where('colors', '#f43f1a');
        $colorReport = TaskType::all()->where('colors', '#8acae7');
        $color = '' ;
    
        if($eventStartData){
            $color = $colorStart ;            
        }
    
        if($eventStopData){
            $color = $colorStop ;
        }
    
        if($eventReportData){
            $color = $colorReport ;
               
        }
    
    }

this is table-display html :

        <div class="row" style="margin-top: 210px;">
            <table class="table" id="">
                    <tr>
                        <th>Program Time</th>
                        <th>Event</th>
                        <th>Message</th>
                        <th>Actual_time</th>
                        <th>Display_Message</th>
                    </tr>
                <tbody >
                @foreach($welcome as $key => $data)
                    <tr>    
                    <td>{{$data->program_time}}</td>
                    <td>{{$data->event}}</td>
                    <td>{{$data->message}}</td> 
                    <td>{{$data->actual_time}}</td>               
                    <td>{{$data->display_message}}</td>                  
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
        <div>

I am stuck and curious to know how i can dynamically change my <td {{$data->event}} element based on the colors I have defined in my index function ?

Source: CSS – Stack Overflow

September 3, 2021
Category : News
Tags: css | laravel

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.