控制器:
public function leaderboard() {
$users = DB::table('users')
->join('images', 'users.id', '=', 'images.user_id')
->select(DB::raw('users.*, COUNT(*) AS totalUploads'))
->orderby('totalUploads', 'desc')
->groupby('users.id')
->take(50)
->get();
return View::make('leaderboard')->with('users', $users);
}
查看:
<tbody>
<?php $i = 0; $rank = 0; $lastCount = null; ?>
@foreach($users as $user)
<?php $i++; if ($user->totalUploads != $lastCount) { $rank = $i; } else { $rank = null; } ?>
<tr>
<td>{{ $rank }}</td>
<td><a href="user/{{ $user->username }}">{{ $user->username }}</a></td>
<td>{{ $user->totalUploads }}</td>
</tr>
<?php $lastCount = $user->totalUploads; ?>
@endforeach
</tbody>
当前输出: (破折号表示<tr>
&#39> s
Rank Name Uploads
-------------------
1 Joe 103
-------------------
2 Dan 99
-------------------
3 Pam 87
-------------------
Kim 87
-------------------
Bob 87
-------------------
6 Mel 60
目标输出: (破折号表示<tr>
&#39> s
Rank Name Uploads
-------------------
1 Joe 103
-------------------
2 Dan 99
-------------------
3 Pam 87
Kim 87
Bob 87
-------------------
6 Mel 60
基本上我想添加一个foreach或其他类型的循环,如果<br />
被绑定,则会在<tr>
$user->totalUploads
内将用户与{{1}}分开。
答案 0 :(得分:0)
编辑:
<tbody>
<?php $i = 0; $rank = 0; $lastCount = null; ?>
@for($i=0; $i<count($users); $i++)
<?php $user = $users[$i];
if ($user->totalUploads != $lastCount) { $rank = $i; } else { $rank = null; }
?>
<tr>
<td>{{ $rank }}</td>
<td>
<a href="user/{{ $user->username }}">{{ $user->username }}</a>
<?php $j=0; ?>
@if(isset($user[$i+1]) && $users[$i+1]->totalUploads == $user->totalUploads)
@for($j=$i+1; $j<count($users)&&$users[$j]->totalUploads == $user->totalUploads; $j++)
<a href="user/{{ $users[$j]->username }}">{{ $users[$j]->username }}</a>
@endfor
@endif
</td>
<td>{{ $user->totalUploads }}
@if($j>0)
@for($k=0; $k<$j-$i; $k++)
{{ $user->totalUploads }}
@endfor
@endif
</td>
</tr>
<?php
$i=$i+$j;
$lastCount = $user->totalUploads; ?>
@endfor
</tbody>
请注意我没有测试过这段代码,即使这看起来很复杂,我认为它会起作用
OLD VERSION
您可以尝试以下内容:
<tbody>
<?php $i = 0; $rank = 0; $lastCount = null; ?>
@foreach($users as $user)
<?php $i++; if ($user->totalUploads != $lastCount) { $rank = $i; } else { $rank = null; } ?>
@if($rank) <tr> @else <br/> @endif
<td>{{ $rank }}</td>
<td><a href="user/{{ $user->username }}">{{ $user->username }}</a></td>
<td>{{ $user->totalUploads }}</td>
@if($rank)</tr> @endif
<?php $lastCount = $user->totalUploads; ?>
@endforeach
</tbody>
如果在foreach中有条件,基本上会添加一个刀片。如果等级没有改变(意思是它在同一&#34;等级&#34;,你不输出但是输出一个
。结束标记只会是当rank不为null(不在同一级别)时应用