Dragos
Dragos

Reputation: 103

Sorting tables in Codeigniter

I've set up a way to sort tables in Codeigniter, but I can only sort them based on only one criteria. For example, I can order them alphabetically by clicking on the name, but I can't also sort them by clicking on the price header.

How can I modify the code so the table sorts the data no matter on what criteria I select?

Here's some snippets of my code.

Model:

function get_carti($sort_by, $sort_order) {
        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('nume_autor, descriere, titlu, pret');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'nume_autor';

        $this->db->select ( 'b.descriere, a.nume_autor, a.id_autor, b.titlu, b.pret, b.image, b.id_carte' );
        $this->db->from ( 'autori a' );
        $this->db->join ( 'carti b', 'a.id_autor = b.id_autor' );
        $this->db->order_by($sort_by, $sort_order);
        $carti = $this->db->get ();
        return $carti->result ();

View:

<div class="table table-striped">
                <table class="table">
                    <?php foreach($fields2 as $field_name => $field_display): ?>
                    <th><?php echo anchor('admin/carti/'.$field_name.'/'.
                            (($sort_order == 'asc' && $sort_by == $field_name) ? 'desc':'asc'), $field_display); ?></th>
                    <?php endforeach;?>

Controller:

function carti($sort_by = 'nume_autor', $sort_order = 'asc'){

        $data['fields2'] = array(
                'titlu' => 'Titlu',
                'nume_autor' => 'Autor',
                'descriere' => 'Descriere',
                'pret' => 'Pret'
        );

        $data['sort_by'] = $sort_by;
        $data['sort_order'] = $sort_order;
        $data['main_content'] = 'carti';
        $data ['carti'] = $this->carti_model->get_carti ($sort_by, $sort_order);
        $this->load->view ( 'page', $data );
    }

Upvotes: 3

Views: 8264

Answers (1)

Daniel Waghorn
Daniel Waghorn

Reputation: 2985

Using CodeIgniter's active record you can order on multiple columns by doing something like this:

$this->db->order_by('title desc, name asc'); 

Where in your case you would do something along these lines:

$this->db->order_by($sort_by_first . ' ' . $order_by_first . ',' .  $sort_by_second . ' ' . $order_by_second'); 

This would return the data sorted on two columns for you to render in your table. It's up to you how you want to implement the anchors to set the sorting criteria on the front end as there's a few possible approaches although it'd need some thought to come up with one that's intuitive to the user.

Alternatively if you are always going to use price as a secondary sort and allow the user to sort on the other columns you could merely go for something like this:

$this->db->order_by($sort_by . ' ' . $sort_order . ', price desc')

Note that you can also simply do multiple order_by calls to add additional columns like:

$this->db->order_by($sort_by_first, $sort_order_first);
$this->db->order_by($sort_by_second, $sort_order_second);

Upvotes: 3

Related Questions