Php 创建类似ol(html)的内容,但取决于搜索结果

Php 创建类似ol(html)的内容,但取决于搜索结果,php,html,model-view-controller,laravel-5.2,Php,Html,Model View Controller,Laravel 5.2,我真的不知道什么是最好的标题,但这里是我想要实现的 我想做的是使第一列像html中的“ol”一样,对搜索结果进行编号 我的刀片上有这个: <table class="table table-bordered" style="text-align: left;"> <tr> <th>Full Name (Last, First, Middle)</th>

我真的不知道什么是最好的标题,但这里是我想要实现的

我想做的是使第一列像html中的“ol”一样,对搜索结果进行编号

我的刀片上有这个:

<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>

全名(姓、首名、中名)
编码的
{!!$searchresults!!}
这是我的控制器

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);  
}
公共函数ListofStaffable(){
$staff=DB::table('staff_profiles')->select('last_name','first_name','middle_name','encoded_by')->orderBy('last_name','ASC')->get();
$searchresults=”“;//定义searchresults变量
foreach($key=>$profile){
$searchresults.=''。
''.  
“.$profile->encoded_by.”。
';
}   
$data=[];
$data['searchresults']=$searchresults;
返回视图(‘用户/主列表/员工列表’,$data);
}
因此,在插入新的th代码后,我应该在控制器中执行什么操作

这是使用Laravel 5.2的方式

刀片模板:

<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>

命令
全名(姓、首名、中名)
编码的
{!!$searchresults!!}
在控制器中:

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);  
}
公共函数listofStaffable()
{   
$staff=DB::table('staff_profiles')->select('last_name','first_name','middle_name','encoded_by')->orderBy('last_name','ASC')->get();
$searchresults=”“;//定义searchresults变量
$i=1;//只需使用在结果上迭代时递增的整数
foreach($key=>$profile)
{           
$searchresults.=''。
“.$i.”。
''.  
“.$profile->encoded_by.”。
'';
$i++;
}
$data=[];
$data['searchresults']=$searchresults;
返回视图(‘用户/主列表/员工列表’,$data);
}

你试图实现目标的方式有点违背“最佳实践”。最好的方法是更好地封装责任。(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);   
}


命令
全名(姓、首名、中名)
编码的
@foreach($key=>$profile的搜索结果)
{{$loop->iteration}
{{$profile->encoded_by}
@endforeach

我没有测试代码,我只是编辑了一下,给你一个想法

谢谢!非常简单:)这完全忽略了刀片的整个点。您不应该将视图特定的逻辑放在控制器中。Blade有一个@foreach关键字,您可以按照VikingCode的回答使用。@RodneyZanoria您的问题是“我应该在控制器中做什么”,而不是“我可以做什么”。。你可以按照Erol的建议去做,但你应该做的是写在我的回答中谢谢你的最佳实践@SebastianOlsen和VikingCode,我现在正试图将其应用到代码中并尝试运行它们。
<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>