php - Sorting tables in Codeigniter -


i've set way sort tables in codeigniter, can sort them based on 1 criteria. example, can order them alphabetically clicking on name, can't sort them clicking on price header.

how can modify code table sorts data no matter on criteria select?

here's snippets of 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 $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 );     } 

using codeigniter's active record can order on multiple columns doing this:

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

where in case along these lines:

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

this return data sorted on 2 columns render in table. it's how want implement anchors set sorting criteria on front end there's few possible approaches although it'd need thought come 1 that's intuitive user.

alternatively if going use price secondary sort , allow user sort on other columns merely go this:

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

note can multiple order_by calls add additional columns like:

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

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -