2011-09-16 1 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

CodeIgniterでは、HTMLテーブル形式でデータを表示するのに最適な表のライブラリを持っています。テーブルにはシリアル番号を追加する必要があることもありますが、Row numはMySQLによって簡単には提供されません。したがって、テーブルライブラリを使用している間は、この機能が追加されてジョブが実行されます。ここで

は、コードを行く:

/** 
    * 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); 

がset_headingで 'いいえSrの' 最初の列を追加することを忘れないでください。 投稿hereを読む。 これが役立つことを願っています。

0

は、クライアント上でこれをやって考えたことはありますか? javascriptライブラリdatatablesは素晴らしいです。ユーザー側でソートを処理でき、ajaxを使用して情報を動的に取得できるため、大きなテーブルでの読み込み時間が短縮されます。 < 100行以下のために、おそらく必要はありません。私はシリアル番号がセマンティックな意味を持っていない限り、それは全く問題なし(AJAXを使用して)

と> 100 000行を持つ/フィルタリングテーブルをソートする必要があり、それをクライアントに表示されません。それらをフィルタリングするだけのものがあります。あなたのコードでは必要だが、ユーザーが見ることができない場合は、dataの属性などで非表示にするだけです。

関連する問題