php 带有 Laravel 的 ElseIf 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25654218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ElseIf statement with laravel
提问by user3732216
I have the following foreach loop that I'm getting good results with however when it gets to the elseif statement I'm attempting to see if two of the players are on the same team or not and if they are then have them listed together separated with a & symbol. Currently I am getting the following still.
我有以下 foreach 循环,我得到了很好的结果,但是当它到达 elseif 语句时,我试图查看两个球员是否在同一支球队中,如果他们是,则将他们分开列出带有 & 符号。目前我仍然收到以下信息。
Player 1 vs. Player 2 vs. Player 3 vs. Player 4
This is okay however Player 1 and Player 2 are on the same team. So its not seeing them as on the same team for some reason. Does someone see what my issue is?
这是可以的,但是玩家 1 和玩家 2 在同一个团队中。因此,出于某种原因,它没有将他们视为同一支球队。有人看到我的问题是什么吗?
@foreach ($match->players AS $key => $player)
@foreach ($player->list as $member)
{{ $member->player_name }}
@endforeach
@if($match->players->count() - 1 != $key)
vs.
@elseif ($match->players[$key - 1]->team_id == $player->team_id)
&
@endif
@endforeach
EDIT: I changed the data a little but still should work.
编辑:我稍微更改了数据,但仍然可以工作。
http://pastebin.com/AdyzemC4
回答by Nein
It was tricky to puzzle together your $match variable :)
将您的 $match 变量拼凑在一起很棘手:)
@if($match->players->count() - 1 != $key)
Your $match->players->count()
is always equal to the value of the number of players(say 'n'). So your 'if
' ONLY checks the 'n-1' != current key ($key
). [Which is true except for the last key, so you get all 'vs'].
你$match->players->count()
总是等于玩家数量的值(比如 'n')。所以你的 ' if
' 只检查 ' n-1' != 当前键 ( $key
)。[这是真的,除了最后一个键,所以你得到所有的'vs']。
This should work:
这应该有效:
@foreach ($match->players AS $key => $player)
@foreach ($player->list as $member)
{{ $member->player_name }}
@endforeach
@if($player->team_id != $match->players[$key + 1]->team_id)
vs.
@elseif ($player->team_id == $match->players[$key + 1]->team_id)
&
@endif
@endforeach
In this we are checking if the current player's team is the same as the next player's team [$key+1
].
在此我们检查当前玩家的团队是否与下一个玩家的团队 [ $key+1
] 相同。
Note:
You need to stop the loop for the last player, since $key+1
will go outside your array and you will get an offset error.
Therefore add another if:
注意:您需要停止最后一个玩家的循环,因为$key+1
它将超出您的数组,并且您将收到偏移错误。因此,如果:
@foreach ($match->players AS $key => $player)
@foreach ($player->list as $member)
{{ $member->player_name }}
@endforeach
@if($key + 1 < $match->players->count())
@if($player->team_id != $match->players[$key + 1]->team_id)
vs.
@elseif ($player->team_id == $match->players[$key + 1]->team_id)
&
@endif
@endif
@endforeach