foreach循环中的多个if语句

时间:2015-06-28 15:18:35

标签: laravel if-statement foreach blade

我的数据库中有3条评论,但不知何故,它只显示3星级评分中的1条已被选中,另外2条评论星级评分为空

@foreach($recensies as $recensie)

                <fieldset class="rating">
                    <input type="radio" disabled  @if($recensie->rating == 5) checked="checked" @endif name="rating" value="5" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == '4 and a half') checked="checked" @endif name="rating" value="4 and a half" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == 4) checked="checked" @endif name="rating" value="4" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == '3 and a half') checked="checked" @endif name="rating" value="3 and a half" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == 3) checked="checked" @endif name="rating" value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == '2 and a half') checked="checked" @endif name="rating" value="2 and a half" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == 2) checked="checked" @endif name="rating" value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == '1 and a half') checked="checked" @endif name="rating" value="1 and a half" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == 1) checked="checked" @endif name="rating" value="1" /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
                    <input type="radio" disabled  @if($recensie->rating == 'half') checked="checked" @endif name="rating" value="half" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
                </fieldset>

@endforeach

这是一张图片,向您展示我的意思。

enter image description here

1 个答案:

答案 0 :(得分:1)

最好使用像这样的星级评分系统:https://github.com/kartik-v/bootstrap-star-rating

那么你只需在@foreach中使用一个输入:

<input id="input-id" type="number" class="rating" min=0 max=5 step=0.5 data-size="lg">

然后致电:

$('#input-id').rating('update', {{ $recensie->rating }});

当然,像Peh说你应该使用像2.5这样的值而不是2 and a half

此外,您还希望为每个输入设置不同的ID。