2016-11-10 50 views
4

我在使用codeigniter。我將數據庫檢索的數據傳遞給我的視圖。我必須在表格中顯示5個列表,每列應包含由視圖中的foreach循環生成的名稱列表。以下是我的觀點代碼。使用foreach排列數據庫在HTML表格中檢索的數據

<table class="table table-hover" > 
    <thead> 
    <tr> 
     <th scope="col" width="20%">Cameramen</th> 
     <th scope="col" width="30%">Camera Assistants</th> 
     <th scope="col" width="12%">Technical Assistants</th> 
     <th scope="col" width="20%">Setup Engineer</th> 
     <th scope="col" width="30%">Audio Operator</th> 
     <th scope="col" width="12%">Vision Operator</th> 
    </tr> 
    </thead> 


    <tbody> 
     <tr> 
     <?php 
     $index = 0; 
     foreach($c_list as $n_key){?> 

      <td><?php echo $index+1; ?> 
      <?php echo $n_key->name; ?><br/><br/> 
      <?php 
       $index++; 
       }?> 
      </td> 

     <?php 
      $index = 0; 
      foreach($ca_list as $n_key){?> 
      <td><?php echo $index+1; ?> 
       <?php echo $n_key->name; ?><br/><br/> 
       <?php 
       $index++; 
       }?> 
      </td> 

      <?php 
      $index = 0; 
       foreach($ta_list as $n_key){?> 
       <td><?php echo $index+1; ?> 
       <?php echo $n_key->name; ?><br/><br/> 
       <?php 
        $index++; 
        }?> 
       </td> 


      <?php 
       $index = 0; 
       foreach($se_list as $n_key){?> 
       <td><?php echo $index+1; ?> 
       <?php echo $n_key->name; ?><br/><br/> 
       <?php 
        $index++; 
        }?> 
       </td> 

      <?php 
       $index = 0; 
       foreach($ao_list as $n_key){?> 
       <td><?php echo $index+1; ?> 
        <?php echo $n_key->name; ?><br/><br/> 
        <?php 
        $index++; 
        }?> 
       </td> 

       <?php 
       $index = 0; 
        foreach($vo_list as $n_key){?> 
        <td><?php echo $index+1; ?> 
        <?php echo $n_key->name; ?><br/><br/> 
        <?php 
         $index++; 
         }?> 
        </td> 

      </tr> 
      </tbody> 
     </table> 

但它給出了以下輸出。

enter image description here

我要顯示在每列由行名行。任何人都可以告訴我錯誤?

從數據庫中檢索數據我在我的模型中編寫了6個函數。他們幾乎相同。我在這裏添加一個模型函數。

public function get_c_names($c) 
{ 
    $cdata = array(); 
    foreach($c as $row) { 
     $cdata[] = $row->employee_id; 
    } 

    $this->db->select('employee.name'); 
    $this->db->from('employee'); 
    $this->db->where('employee.id IN ('.implode(", ",$cdata).')'); 
    $query=$this->db->get(); 
    return $query->result(); 
} 

在控制器中,我用下面的代碼調用這個函數,然後它傳遞給查看如下。所有6個功能都有相同的模式。所以我只發佈其中的一個。

$name['c_list'] = $this->employee_model->get_c_names($c); 
$this->load->view('team_view',$name); 
+0

如果您向我們展示這些'$ ?? _ list'數組中的每個元素,那麼有人可能會幫助您。如果不是我們有點在黑暗中工作,並且所有你會得到**猜測**我想我們可以猜到它們是什麼,但是如果你真的告訴我們它會更好 – RiggsFolly

+0

如果這些數組是通過查詢數據庫構建的,那麼你也可以告訴我們該代碼,因爲它最有可能的解決辦法是以更好的方式做到這一點 – RiggsFolly

+0

@RiggsFolly我已經更新了答案。請檢查一下。 – Christeen

回答

0
<table class="table table-hover" > 
<thead> 
<tr> 
    <th scope="col" width="20%">Cameramen</th> 
    <th scope="col" width="30%">Camera Assistants</th> 
    <th scope="col" width="12%">Technical Assistants</th> 
    <th scope="col" width="20%">Setup Engineer</th> 
    <th scope="col" width="30%">Audio Operator</th> 
    <th scope="col" width="12%">Vision Operator</th> 
</tr> 
</thead> 


<tbody> 


    <?php 
    for($x = 0 ; $x < count($c_list) ; $x++){ 
     echo '<tr>'; 
      echo '<td>'. $c_list[$x] .'</td>'; 
      echo '<td>'. $ca_list[$x] .'</td>'; 
      echo '<td>'. $ta_list[$x] .'</td>'; 
      echo '<td>'. $se_list[$x] .'</td>'; 
      echo '<td>'. $ao_list[$x] .'</td>'; 
      echo '<td>'. $vo_list[$x] .'</td>'; 
     echo '</tr>'; 
    } 

    ?> 

     </tbody> 
    </table> 

如果所有的列表具有相同的長度,這是一個解決方案。

+0

它的長度不相同。不管怎樣,謝謝。 – Christeen

1
<table class="table table-hover" > 
<thead> 
<tr> 
    <th scope="col" width="20%">Cameramen</th> 
    <th scope="col" width="30%">Camera Assistants</th> 
    <th scope="col" width="12%">Technical Assistants</th> 
    <th scope="col" width="20%">Setup Engineer</th> 
    <th scope="col" width="30%">Audio Operator</th> 
    <th scope="col" width="12%">Vision Operator</th> 
</tr> 
</thead> 


<tbody> 


<?php 

// find the longest array 
$max = max(count($c_list), count($ca_list), count($ta_list), count($se_list), count($ao_list), count($vo_list)); 

for($x = 0 ; $x < $max ; $x++){ 
    echo '<tr>'; 
     echo '<td>'. (isset($c_list[$x])?$c_list[$x]:'') .'</td>'; 
     echo '<td>'. (isset($ca_list[$x])?$ca_list[$x]:'') .'</td>'; 
     echo '<td>'. (isset($ta_list[$x])?$ta_list[$x]:'') .'</td>'; 
     echo '<td>'. (isset($se_list[$x])?$se_list[$x]:'') .'</td>'; 
     echo '<td>'. (isset($ao_list[$x])?$ao_list[$x]:'') .'</td>'; 
     echo '<td>'. (isset($vo_list[$x])?$vo_list[$x]:'') .'</td>'; 
    echo '</tr>'; 
} 

?> 

    </tbody> 
</table> 

這項工作的多長度數組。

相關問題