通过单击列标题按列排序数据,然后使用codeigniter再次单击以反向排序

时间:2013-03-15 07:08:18

标签: php codeigniter sorting

嘿伙计们我是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>

1 个答案:

答案 0 :(得分:0)

有一个很好的教程,在CodeIgniter中从头开始“系列:

http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-displaying-sorting-tabular-data/

相关问题