title: Sorting tables in Codeigniter
body: <p>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.</p>
  
  <p>How can I modify the code so the table sorts the data no matter on what criteria I select?</p>
  
  <p>Here's some snippets of my code.</p>
  
  <p>Model:</p>
  
  <pre><code>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-&gt;db-&gt;select ( 'b.descriere, a.nume_autor, a.id_autor, b.titlu, b.pret, b.image, b.id_carte' );
          $this-&gt;db-&gt;from ( 'autori a' );
          $this-&gt;db-&gt;join ( 'carti b', 'a.id_autor = b.id_autor' );
          $this-&gt;db-&gt;order_by($sort_by, $sort_order);
          $carti = $this-&gt;db-&gt;get ();
          return $carti-&gt;result ();
  </code></pre>
  
  <p>View: </p>
  
  <pre><code>&lt;div class="table table-striped"&gt;
                  &lt;table class="table"&gt;
                      &lt;?php foreach($fields2 as $field_name =&gt; $field_display): ?&gt;
                      &lt;th&gt;&lt;?php echo anchor('admin/carti/'.$field_name.'/'.
                              (($sort_order == 'asc' &amp;&amp; $sort_by == $field_name) ? 'desc':'asc'), $field_display); ?&gt;&lt;/th&gt;
                      &lt;?php endforeach;?&gt;
  </code></pre>
  
  <p>Controller: </p>
  
  <pre><code>function carti($sort_by = 'nume_autor', $sort_order = 'asc'){
  
          $data['fields2'] = array(
                  'titlu' =&gt; 'Titlu',
                  'nume_autor' =&gt; 'Autor',
                  'descriere' =&gt; 'Descriere',
                  'pret' =&gt; 'Pret'
          );
  
          $data['sort_by'] = $sort_by;
          $data['sort_order'] = $sort_order;
          $data['main_content'] = 'carti';
          $data ['carti'] = $this-&gt;carti_model-&gt;get_carti ($sort_by, $sort_order);
          $this-&gt;load-&gt;view ( 'page', $data );
      }
  </code></pre>