2011-09-16 21 views
0

我知道如何創建分頁並生成包含codeigniter中的數據庫信息的表,但我不知道如何顯示錶中每一行的序列號。例如:如何在使用codeigniter中的表庫創建的表中顯示序列號?

SL. Name   Email 
1. Srijon  [email protected] 
2. Jake  [email protected] 

請您告訴我該怎麼做?在此先感謝

下面是我如何創建分頁和生成表的控制器(無序列號)

function index(){ 

      $this->load->library('pagination'); 
      $this->load->library('table'); 
      $this->table->set_heading('Student ID','Student Name','Batch','Edit','Delete'); 

      $config['base_url'] = 'http://localhost/coaching/index.php/student_list/index'; 
      $config['total_rows'] = $this->db->get('student')->num_rows(); 
      $config['per_page'] = 15; 
      $config['num_links'] = 20; 
      $config['full_tag_open'] = '<div id="pagination" align="center">'; 
      $config['full_tag_close'] = '</div>'; 

      $this->pagination->initialize($config); 
      $data['tab'] = "Student List";    
      $this->load->model('mod_studentlist'); 
      $data['records']= $this->mod_studentlist->student_list(); 
      $data['main_content']='studentlist'; 
      $this->load->view('includes/template',$data); 

     } 

這裏是我的模型

   function student_list() 
    { 
     $config['per_page'] = 10; 
     $this->db->select('studentid, studentname, batch'); 
     $this->db->order_by("studentid", "desc"); 
     $rows = $this->db->get('student',$config['per_page'],$this->uri->segment(3))->result_array(); 


     foreach ($rows as $count => $row) 
      { 
      $rows[$count]['studentname'] = anchor('student_list/get/'.$row['studentid'],$row['studentname']); 
      $rows[$count]['Edit'] = anchor('update_student/update/'.$row['studentid'],'Update'); 
      $rows[$count]['Delete'] = anchor('report/'.$row['studentid'],'Delete'); 

      } 
     return $rows; 
    } 

這是我的看法文件

    <?php echo $this->table->generate($records);?> 
        <?php echo $this->pagination->create_links();?> 
        <script type="text/javascript" charset="utf-8"> 
         $('tr:odd').css('background','#EAEAEA'); 
         </script> 

回答

2

您應該在模型函數中創建一個變量:

<?php 
function student_list() 
{ 
    $config['per_page'] = 10; 
    $this->db->select('studentid, studentname, batch'); 
    $this->db->order_by("studentid", "desc"); 
    $rows = $this->db->get('student',$config['per_page'],$this->uri->segment(3))->result_array(); 

    $sl = $this->uri->segment(3) + 1; // so that it begins from 1 not 0 

    foreach ($rows as $count => $row) 
     { 
     array_unshift($rows[$count], $sl.'.'); 
     $sl = $sl + 1; 

     $rows[$count]['studentname'] = anchor('student_list/get/'.$row['studentid'],$row['studentname']); 
     $rows[$count]['Edit'] = anchor('update_student/update/'.$row['studentid'],'Update'); 
     $rows[$count]['Delete'] = anchor('report/'.$row['studentid'],'Delete'); 
     } 
    return $rows; 
} 

和修改你的控制器:

<?php 
function index() { 
    $this->load->library('pagination'); 
    $this->load->library('table'); 
    $this->table->set_heading('SL.', 'Student ID','Student Name','Batch','Edit','Delete'); 
    .... 
+0

非常感謝您的幫助。我正在獲取序列號,但它顯示在批次列下,這是假設在SL列下顯示的。請您好好看看這件事情​​嗎?在此先感謝:) –

+0

我修改了代碼。使用array_unshift函數將元素添加到數組的開頭,這使序列號顯示在第一列中。 – cenanozen

+0

非常感謝Cenanozen。它現在工作完美。 :) –

0

笨有很大的表庫,以顯示HTML表格格式的數據。添加一個序列號有時需要一個表,但是Row num不是由MySQL提供的,因此在使用Table庫的時候添加了這個功能來完成這項工作。

這裏去代碼:

/** 
    * Set the serial no 
    * 
    * Add serial no to left of table 
    * 
    * @access public 
    * @param mixed 
    * @return mixed 
    */ 
    function add_sr_no(&$arr) 
    { if (!is_array($arr)) return $arr; 
    $i=1; 
    foreach ($arr as &$values) {   
     if (!is_array($values)) 
     break; 
     else 
     array_unshift($values, $i++); 
    }  
    return $arr; 
    } 

只需添加這些行庫table.php。 用法:

$rows = $query->result_array(); 
$this->table->add_sr_no($rows); 

不要忘記添加第一列 'Sr否' 的set_heading。 閱讀文章here。 希望這有助於。

0

您是否想過在客戶端上做這件事? javascript庫datatables非常棒。它可以在用戶端爲您處理排序,並且可以使用ajax動態檢索信息,因此大型表上的加載時間很快。因爲在<以下100行左右可能並不需要。我有它排序/過濾表> 100000行,沒有任何問題(使用AJAX)

除非序列號具有語義含義不顯示他們的客戶端。它只是讓他們過濾更多東西。如果它們是您的代碼所需的,但不是供用戶查看的,請將它們隱藏在data屬性或其他內容中。

相關問題