嘿伙计们,我是 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%;"> </td>
<td width="5%;"> </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>