在视图中使用foreach遍历学生信息
一、效果图
二、代码部分
1.PhotoController.php
class PhotoController extends Controller{
public $students=[('引入学生信息')];
@return \Illuminate\Http\Response
public function index(){
$students=$this->students;
$id=null;
$xingming=null;
$xh=null;
$zhuanye=null;
$xingbie=null;
$banji=null;
$data= compact ('students','id','xingming','xh','zhuanye','xingbie','banji');
return view('info',$data);
}
}
2.info.blade.php
<body>
<h3 style="text-align: center;">学生信息</h3>
<table class="table2_4 " style="text-align: center;" align="center"border=1; >
<tr>
<th>编号</th>
<th>学号</th>
<th>姓名</th>
<th>专业</th>
<th>班级</th>
<th>性别</th>
</tr>
@foreach ($students as $item)
<tr align="center" >
<td>{{$item['id']}} </td>
<td>{{$item['xh']}} </td>
<td>{{$item['xingming']}}</td>
<td>{{$item['zhuanye']}}</td>
<td>{{$item['banji']}}</td>
<td>{{$item['xingbie']}}</td>
</tr>
@endforeach
</table>
</body>
<style type="text/css">
.table2_4 table {
width:100%;
margin:15px 0
}
.table2_4 th {
background-color:#C3E7FA;
color:#000000;
}
.table2_4,.table2_4 th,.table2_4 td
{
font-size:20px;
text-align:center;
padding:4px;
border:1px solid #dcf1fc;
border-collapse:collapse
}
/* odd 奇数 */
.table2_4 tr:nth-child(odd){
background-color:#ebf7fd;
}
/* 偶数 */
.table2_4 tr:nth-child(even){
background-color:#fdfdfd;
}
</style>
3.补充
九九乘法表:
@for ($i=1 ; $i<=9 ; $i++)
@for ($j=1 ; $j<=$i ; $j++)
{{$i}}*{{$j}} = {{$i*$j}}
@endfor <br>
@endfor
视图中的注释方式
html中的 注释方法无法在视图注释引入的变量 如:{{$item[‘xh’]}}
只能使用 {{-- $item[‘xh’] --}}的方式进行注释 在括号里的变量两侧各加两个“ - ”