2015-05-01 18 views
3

我想使用codeigniter在數據庫中提取數據。在數據庫中取數據的同時對錶進行編號

我爲每個數據生成一個錶行,並希望在實際數據描述之前編號。

這是我的看法: 請注意,<td>#</td><tbody></tbody>。那是我想要編號的地方。

<div class="table-responsive"> 
     <table class="table table-hover table-bordered table-striped table-condensed"> 
      <caption><h3><strong>Master List of Suppliers</strong></h3></caption> 
      <thead> 
       <tr> 
        <th>#</th> 
        <th>Supplier Code</th> 
        <th>Description</th> 
        <th>Address</th> 
       </tr> 
      </thead> 
      <tbody> 
      <?php 
       foreach ($view_masterlist_suppliers as $row) { 
        echo 
         '<tr> 
          <td>#</td> 
          <td><a href="'.site_url("site/itemspersupplier?i=".$row->SUP_CODE).'">'.$row->SUP_CODE.'</a></td> 
          <td>'.$row->SUP_DESC.'</td>'; 
          if(empty($row->SUP_ADD1)){ echo '<td><strong>- NO ADDRESS HAS BEEN ENCODED -</strong></td>';} 
          else if(empty($row->SUP_ADD2)){ echo '<td>'.$row->SUP_ADD1.'</td>';} 
          else{ echo '<td>'.$row->SUP_ADD1.', '.$row->SUP_ADD2.'</td>';} 
        echo '</tr>'; 
       } 
      ?> 
      </tbody> 
     </table> 
    </div> 

這是我的控制器:

public function itemspersupplier(){   
     $data["title"] = "Supplier Master List"; 

     $i = $_GET['i']; 
     $this->load->model("get_model"); 
     $data["view_items_per_suppliers"] = $this->get_model->view_items_per_suppliers($i); 

     $this->load->view("templates/header", $data); 
     $this->load->view("templates/nav", $data); 
     $this->load->view("pages/items_per_supplier", $data); 
     $this->load->view("templates/footer", $data); } 

這是我的模型:注:我有數據庫,但沒有什麼可擔心的吧:)

function view_items_per_suppliers($i){ 
     //Load db2 and run query 
     $CI = &get_instance(); 
     //Seeting the second parameter to TRUE (Boolean) the function will 
     //return the database object. 
     $this->db2 = $CI->load->database('db2', TRUE); 
     $this->db2->select('TBL_SUPPLIER.SUP_CODE,TBL_SUPPLIER.SUP_DESC,TBL_SUPPLIER.SUP_ADD1,TBL_SUPPLIER.SUP_ADD2,TBL_SUPPLIER.SUP_TEL1,TBL_SUPPLIER.SUP_TEL2,TBL_SUPPLIER.SUP_FAX1,TBL_SUPPLIER.SUP_FAX2,TBL_SUPPLIER.SUP_CONT,TBL_MASTER_ITEMS.IN_CODE,TBL_MASTER_ITEMS.ITE_DESC,TBL_MASTER_ITEMS.UNIT,TBL_MASTER_ITEMS.UNT_COST'); 
     $this->db2->from('TBL_SUPPLIER'); 
     $this->db2->join('TBL_MASTER_ITEMS', 'TBL_SUPPLIER.SUP_CODE = TBL_MASTER_ITEMS.SUP_CODE', 'inner'); 
     $this->db2->where('TBL_MASTER_ITEMS.SUP_CODE', $_GET['i']); 
     //$this->db->limit('25'); 

     $view_items_per_suppliers = $this->db2->get(); 
     return $view_items_per_suppliers->result(); 
    } 

感謝。

回答

1

您可以使用任何

foreach ($view_masterlist_suppliers as $index=>$row) { 
     echo 
     '<tr> 
      <td>'.$index+1.'</td> 
      //your rest code 

或者

$index=0; 
    foreach ($view_masterlist_suppliers as $index=>$row) { 
     echo 
     '<tr> 
      <td>'.++$index.'</td> 
      //your rest code 
+0

謝謝,這是工作....太棒了! – Markmeplease

1

只需更新您的代碼即可。這會爲你工作

<tbody> 
<?php 
    $i = 1; 
    foreach ($view_masterlist_suppliers as $row) { 
     echo 
     '<tr> 
     <td>'.$i++.'</td> 
     <td><a href="'.site_url("site/itemspersupplier?i=".$row->SUP_CODE).'">'.$row->SUP_CODE.'</a></td> 
     <td>'.$row->SUP_DESC.'</td>'; 
     if(empty($row->SUP_ADD1)){ echo '<td><strong>- NO ADDRESS HAS BEEN ENCODED -</strong></td>';} 
     else if(empty($row->SUP_ADD2)){ echo '<td>'.$row->SUP_ADD1.'</td>';} 
     else{ echo '<td>'.$row->SUP_ADD1.', '.$row->SUP_ADD2.'</td>';} 
       echo '</tr>'; 
     } 
?>  

可簡單地實現爲

foreach ($view_masterlist_suppliers as $key => $row) { 
echo 
'<tr> 
<td>'.$key+1.'</td> 

OR

foreach ($view_masterlist_suppliers as $key => $row) { 
echo 
'<tr> 
<td>'.++$key.'</td> 
+0

它給我在該行下面的語法錯誤。 – Markmeplease

+0

@Markmeplease你得到了什麼語法錯誤?寫完整的錯誤信息。 –

+0

@Narendra西索迪亞我想你錯過遇到'$ i' –

相關問題