Sorting tables in Codeigniter

I've created a way to sort tables in Codeigniter, but I can only sort them by one criterion. For example, I can sort them alphabetically by clicking on the name, but I also cannot sort them by clicking on the price heading.

How can I change the code so that the table sorts the data no matter what criteria I choose?

Here are 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 );
    }

      

+3


source to share


1 answer


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

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

      

If in your case you do something in this direction:

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

      

This will return data sorted by two columns for rendering in your table. It depends on how you want to implement the bindings in order to set the sort criteria on the front panel, as there are several possible approaches, although it took some thought to create one that is intuitive to the user.



Alternatively, if you will always use price as a secondary sort and allow the user to sort by other columns, you can simply go something like this:

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

      

Note that you can also just make a few calls order_by

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

      

+3


source







All Articles