问题描述
我的观点
<table class="table">
@foreach($students as $student)
<thead>
<tr style="background-color: #e9e5e4;">
<td colspan="4">{{$student->teacher_name}}</td>
</tr>
<tr>
<th> Name </th>
<th> class</th>
<th> subject </th>
</tr>
</thead>
<tbody>
<tr>
<td> {{$student->name }}</td>
<td> {{$student->class}} </td>
<td> {{$student->subject }} </td>
</tr>
</tbody>
@endforeach
</table>
即使老师姓名相同,它也会显示多个表,我只想在更改老师姓名时创建新表,否则将所有记录显示在同一个表中
解决方法
你把循环放错了。试试这个
def average(x):
return sum(x) / len(x)
def final_list(lists):
return [average(x) for x in lists]
,
试试以下方法:
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>Subject</th>
</tr>
</thead>
<tbody>
@foreach($students as $student)
<tr style="background-color: #e9e5e4;">
<td colspan="4">{{$student->teacher_name}}</td>
</tr>
<tr>
<td>{{$student->name }}</td>
<td>{{$student->class}} </td>
<td>{{$student->subject }} </td>
</tr>
@endforeach
</tbody>
</table>
OR
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>Subject</th>
<th>Teacher</th>
</tr>
</thead>
<tbody>
@foreach($students as $student)
<tr>
<td>{{$student->name }}</td>
<td>{{$student->class}} </td>
<td>{{$student->subject }} </td>
<td style="background-color: #e9e5e4;">{{$student->teacher_name}}</td>
</tr>
@endforeach
</tbody>
</table>