根据幼虫叶片的状况进行着色

时间:2019-01-25 06:55:47

标签: php css laravel

我正在尝试对数组元素的值使用条件来为div设置背景颜色。

问题是我的语法在某处是错误的,因为正确的颜色正在转储带有周围的标签,但实际上并没有将div设置为正确的颜色。看来条件在起作用,但我的语法在某个地方坏了

@foreach($placedList as $placed)
    @if ($placed['AGING'] < 30) 
       $color = "green";
    @elseif ($placed['AGING'] > 30 and $placed['AGING'] < 60) 
       $color = "yellow"
    @else
       $color = "red";
    @endif

    <div class="uk-width-1-10" style="background-color:{{$color}}">{{$placed['AGING']}}</div>

@endforeach

要真正应用此功能需要更改什么?

1 个答案:

答案 0 :(得分:2)

使用以下代码替换您的代码

@foreach($placedList as $placed)

    @php

    if ($placed['AGING'] < 30) {
       $color = "green";
    }elseif ($placed['AGING'] > 30 and $placed['AGING'] < 60) {
       $color = "yellow";
    }else{
       $color = "red";
    }

    @endphp

    <div class="uk-width-1-10" style="background-color:{{$color}}">{{$placed['AGING']}}</div>

@endforeach