2013-03-15 31 views
0

排序数据嘿家伙我是codeigniter中的新手,我正在从事数据库工作,我有一个表feuhed ..我想排序表通过点击表头我怎么能做到这一点。 。我使用的是这样的代码: -通过点击列标题来逐列排序数据,并通过再次点击使用codeigniter

控制器:

function index() 
{ 

     $config['total_rows'] = $this->db->get('tbl_members')-> num_rows(); 
     $config['per_page'] = 2; 

     $segment_array=$this->uri->segment_array(); 
     $segment_count=$this->uri->total_segments(); 

     $do_orderby = array_search("orderby",$segment_array); 
     $asc = array_search("asc",$segment_array); 
     $desc = array_search("desc",$segment_array); 

     $this->db->order_by($this->uri->segment($do_orderby+1), $this->uri->segment($do_orderby+2)); 

     if (ctype_digit($segment_array[$segment_count])) 
     { 
      $data['page']=NULL; 
      $this->db->limit($config['per_page']); 
     } 
     else 
     { 
      $data['page']=$segment_array[$segment_count]; 
      $this->db->limit($config['per_page'], $segment_array[$segment_count]); 
      array_pop($segment_array); 
     } 

     $config['base_url'] = site_url(join("/",$segment_array)); 
     $config["uri_segment"] = count($segment_array)+1; 

     $this->load->model('mod_user'); //load the mod_user class 
     $data['rows'] = $this->mod_user->getmembers(); 
     //initialize pagination 
     $this->pagination->initialize($config); 

     $this->load->view('view_home',$data); 
     $this->load->view('view_homemember',$data); 
    } 

我的看法是:

<form class="userinfo" action="" method="post"> 
    <?php //if(count($rows) > 0) { ?> 
     <table border="1" cellpadding="2" cellspacing="0"> 
        <?php 
    // For Sorting 
    //default in descending order 
    $sort['col1']='desc'; 
    $sort['col2']='desc'; 
    $sort['col3']='desc'; 
    //get the segment array 
    $segment_array=$this->uri->segment_array(); 
    //search for the orderby string 
    $do_orderby = array_search("orderby",$segment_array); 
    //check to toggle asc and desc sorting in columns 
    if($do_orderby !== FALSE) { 
    $sort[$segment_array[$do_orderby+1]]= $segment_array[$do_orderby + 2] == 'desc' ? 'asc' : 'desc' ; 
    } 
    ?> 
      <tr> 
       <td width="5%;">&nbsp;</td> 
       <td width="5%;">&nbsp;</td> 
       <td width="15%;"><?php echo "<a href=\"".site_url()."/ctl_home/index/member_name/{$sort['col1']}/".$page."\">";?>Name</a></td> 
       <td width="15%;">Moderator Name</td> 
       <td width="20%;">KCC Branch</td> 
       <td width="15%;">Father/Husband Name</td> 
       <td width="15%;">Address</td> 
       <td width="10%;">Date</td> 
      </tr> 

回答