创建类似 ol (html) 的内容,但取决于搜索结果
Creating something like ol (html) but depending on search results
我真的想不出什么是最好的标题,但这是我想要实现的目标。
我想要做的是让第一列像 html 中的 'ol' 一样,它对搜索结果进行编号。
我的blade里有这个:
<table class="table table-bordered" style="text-align: left;">
<tr>
<th>Full Name (Last, First, Middle)</th>
<th>Encoded by</th>
</tr>
<tbody>
{!! $searchresults !!}
</tbody>
</table>
这是我的控制器
public function listofStaffTable(){
$staffs = DB::table('staff_profiles')->select('last_name','first_name','middle_name','encoded_by')->orderBy('last_name','ASC')->get();
$searchresults = ""; // define the searchresults variable
foreach( $staffs as $key => $profile ){
$searchresults .= '<tr>'.
'<td><a href="'. URL::route('viewstaffprofile', $profile->id) .'">'. $profile->last_name .' '. $profile->first_name .' '. $profile->middle_name .' </a></td>'.
'<td>'. $profile->encoded_by .'</td>'.
</tr>';
}
$data = [];
$data['searchresults'] = $searchresults;
return view( 'user/masterlist/list-of-staffs', $data);
}
那么在插入新的th代码后,我应该在我的控制器中做什么。
顺便说一句,这是在使用Laravel 5.2
Blade 模板:
<table class="table table-bordered" style="text-align: left;">
<tr>
<th>Order</th>
<th>Full Name (Last, First, Middle)</th>
<th>Encoded by</th>
</tr>
<tbody>
{!! $searchresults !!}
</tbody>
</table>
在你的控制器中:
public function listofStaffTable()
{
$staffs = DB::table('staff_profiles')->select('last_name','first_name','middle_name','encoded_by')->orderBy('last_name','ASC')->get();
$searchresults = ""; // define the searchresults variable
$i = 1; // just use a integer that increments when iterating over results
foreach( $staffs as $key => $profile )
{
$searchresults .= '<tr>'.
'<td>'.$i.'.</td>'.
'<td><a href="'. URL::route('viewstaffprofile', $profile->id) .'">'. $profile->last_name .' '. $profile->first_name .' '. $profile->middle_name .' </a></td>'.
'<td>'. $profile->encoded_by .'</td>'.
'</tr>';
$i++;
}
$data = [];
$data['searchresults'] = $searchresults;
return view( 'user/masterlist/list-of-staffs', $data);
}
您尝试实现目标的方式有点违背 'best practice'。最好的方法是更好地封装责任。 (html 生成属于您控制器中的视图和逻辑)
所以从你的控制器中删除所有 html 并在你的视图中进行生成(迭代)部分......你应该在那里迭代并生成你的 html
public function listofStaffTable()
{
$staffs = DB::table('staff_profiles')
->select('last_name','first_name','middle_name','encoded_by')
->orderBy('last_name','ASC')
->get();
$data = [];
$data['searchresults'] = $searchresults;
return view( 'user/masterlist/list-of-staffs', $data);
}
<table class="table table-bordered" style="text-align: left;">
<tr>
<th>Order</th>
<th>Full Name (Last, First, Middle)</th>
<th>Encoded by</th>
</tr>
<tbody>
@foreach ($searchresults as $key => $profile )
<tr>
<!-- The current loop iteration (starts at 1). -->
<td> {{ $loop->iteration }} </td>
<td>
<a href="{{ route('viewstaffprofile', $profile->id) }} ">
{{ $profile->last_name }} {{ $profile->first_name }} {{ $profile->middle_name }}
</a>
</td>
<td> {{ $profile->encoded_by }} </td>
</tr>
@endforeach
</tbody>
</table>
我没有测试代码,我只是编辑一下给你一个想法。
我真的想不出什么是最好的标题,但这是我想要实现的目标。
我想要做的是让第一列像 html 中的 'ol' 一样,它对搜索结果进行编号。
我的blade里有这个:
<table class="table table-bordered" style="text-align: left;">
<tr>
<th>Full Name (Last, First, Middle)</th>
<th>Encoded by</th>
</tr>
<tbody>
{!! $searchresults !!}
</tbody>
</table>
这是我的控制器
public function listofStaffTable(){
$staffs = DB::table('staff_profiles')->select('last_name','first_name','middle_name','encoded_by')->orderBy('last_name','ASC')->get();
$searchresults = ""; // define the searchresults variable
foreach( $staffs as $key => $profile ){
$searchresults .= '<tr>'.
'<td><a href="'. URL::route('viewstaffprofile', $profile->id) .'">'. $profile->last_name .' '. $profile->first_name .' '. $profile->middle_name .' </a></td>'.
'<td>'. $profile->encoded_by .'</td>'.
</tr>';
}
$data = [];
$data['searchresults'] = $searchresults;
return view( 'user/masterlist/list-of-staffs', $data);
}
那么在插入新的th代码后,我应该在我的控制器中做什么。
顺便说一句,这是在使用Laravel 5.2
Blade 模板:
<table class="table table-bordered" style="text-align: left;">
<tr>
<th>Order</th>
<th>Full Name (Last, First, Middle)</th>
<th>Encoded by</th>
</tr>
<tbody>
{!! $searchresults !!}
</tbody>
</table>
在你的控制器中:
public function listofStaffTable()
{
$staffs = DB::table('staff_profiles')->select('last_name','first_name','middle_name','encoded_by')->orderBy('last_name','ASC')->get();
$searchresults = ""; // define the searchresults variable
$i = 1; // just use a integer that increments when iterating over results
foreach( $staffs as $key => $profile )
{
$searchresults .= '<tr>'.
'<td>'.$i.'.</td>'.
'<td><a href="'. URL::route('viewstaffprofile', $profile->id) .'">'. $profile->last_name .' '. $profile->first_name .' '. $profile->middle_name .' </a></td>'.
'<td>'. $profile->encoded_by .'</td>'.
'</tr>';
$i++;
}
$data = [];
$data['searchresults'] = $searchresults;
return view( 'user/masterlist/list-of-staffs', $data);
}
您尝试实现目标的方式有点违背 'best practice'。最好的方法是更好地封装责任。 (html 生成属于您控制器中的视图和逻辑)
所以从你的控制器中删除所有 html 并在你的视图中进行生成(迭代)部分......你应该在那里迭代并生成你的 html
public function listofStaffTable()
{
$staffs = DB::table('staff_profiles')
->select('last_name','first_name','middle_name','encoded_by')
->orderBy('last_name','ASC')
->get();
$data = [];
$data['searchresults'] = $searchresults;
return view( 'user/masterlist/list-of-staffs', $data);
}
<table class="table table-bordered" style="text-align: left;">
<tr>
<th>Order</th>
<th>Full Name (Last, First, Middle)</th>
<th>Encoded by</th>
</tr>
<tbody>
@foreach ($searchresults as $key => $profile )
<tr>
<!-- The current loop iteration (starts at 1). -->
<td> {{ $loop->iteration }} </td>
<td>
<a href="{{ route('viewstaffprofile', $profile->id) }} ">
{{ $profile->last_name }} {{ $profile->first_name }} {{ $profile->middle_name }}
</a>
</td>
<td> {{ $profile->encoded_by }} </td>
</tr>
@endforeach
</tbody>
</table>
我没有测试代码,我只是编辑一下给你一个想法。