2017-04-27 82 views
0

我真的搞不清楚什麼是最好的標題,但這是我想要實現的。創建一些像ol(html),但取決於搜索結果

我想要做的就是讓第一列像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> 

這是我的控制器

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); 
} 

所以插入一個新的次代碼之後,我應該在我的控制器做。

這是使用Laravel 5.2的方式

回答

0

刀片模板:

<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); 
} 
+0

謝謝!非常簡單:) –

+2

這完全忽略了刀片的全部重點。你不應該把視圖特定的邏輯放在控制器中。 Blade有一個@foreach關鍵字,您可以像VikingCode的答案中指定的那樣使用它。 –

+0

@RodneyZanoria @RodneyZanoria你的問題是「我應該在我的控制器中做什麼」而不是「我能做什麼」..你可以做什麼Erol提出的,但你應該做的是寫在我的回答 – VikingCode

1

你努力實現自己的目標的方式是對位'最佳實踐'。最好的辦法是將責任更好地封裝起來。 (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> 

我沒有測試的代碼,我剛纔編輯給你的想法。