title: How To Create Sortable Table THEAD Links for Codeigniter Table
body: <p>I can not seem to make my thead th links be able to sort out my data. I have tried many different methods now not sure what to do.</p>
  
  <p><strong>For example</strong> if user clicks on the user id thead link it will sort users by user id asc or desc and so on. Same for username, date added links. I have tried it with out sourced scripts but because it is controlled with controller does not work.</p>
  
  <p><strong>Using My Code</strong></p>
  
  <p>What I am trying to achieve is with the user list table head area there are <strong>5 thead</strong> that I would like to make it so when I click on either one it sorts out the list according to that field by asc and or or dsc. Trying to make it work with current pagination the <strong>pagination works fine</strong>.  </p>
  
  <p>Some Library's are auto-loaded.</p>
  
  <p>Codeigniter User Controller</p>
  
  <pre><code>protected function getList() {
      $data['heading_title'] = $this-&gt;lang-&gt;line('heading_title');
  
      $data['breadcrumbs'] = array();
  
      $data['breadcrumbs'][] = array(
          'text' =&gt; '&lt;i class="fa fa-home"&gt;&lt;/i&gt;' .' '.  $this-&gt;lang-&gt;line('text_home'),
          'href' =&gt; site_url('admin/dashboard')
      );
  
      $data['breadcrumbs'][] = array(
          'text'      =&gt; $this-&gt;lang-&gt;line('heading_title'),
          'href'      =&gt; site_url('admin/users')
      );
  
      $data['text_enabled'] = $this-&gt;lang-&gt;line('text_enabled');
      $data['text_disabled'] = $this-&gt;lang-&gt;line('text_disabled');
  
      $data['column_user_id'] = $this-&gt;lang-&gt;line('column_user_id');
      $data['column_name'] = $this-&gt;lang-&gt;line('column_name');
      $data['column_status'] = $this-&gt;lang-&gt;line('column_status');
      $data['column_last_updated'] = $this-&gt;lang-&gt;line('column_last_updated');
      $data['column_date_added'] = $this-&gt;lang-&gt;line('column_date_added');
      $data['column_action'] = $this-&gt;lang-&gt;line('column_action');
  
      $data['delete'] = site_url('admin/users/delete');
      $data['insert'] = site_url('admin/users/add');
  
      $data['text_confirm'] = $this-&gt;lang-&gt;line('text_confirm');
  
      $data['button_insert'] = $this-&gt;lang-&gt;line('button_insert');
      $data['button_edit'] = $this-&gt;lang-&gt;line('button_edit');
      $data['button_delete'] = $this-&gt;lang-&gt;line('button_delete');
  
      $this-&gt;load-&gt;library('setting'); 
  
      $this-&gt;load-&gt;library('pagination'); 
  
      $config = array(); 
      $config["base_url"] = base_url('admin/users'); 
      $config['total_rows'] = $this-&gt;db-&gt;get('user')-&gt;num_rows(); 
      $config["per_page"] =  $this-&gt;setting-&gt;get('config_limit_admin');
      $config["uri_segment"] = 3;  
  
      $config['full_tag_open'] = "&lt;ul class='pagination'&gt;";
      $config['full_tag_close'] ="&lt;/ul&gt;";
      $config['num_tag_open'] = '&lt;li&gt;';
      $config['num_tag_close'] = '&lt;/li&gt;';
      $config['cur_tag_open'] = "&lt;li class='disabled'&gt;&lt;li class='active'&gt;&lt;a href='#'&gt;";
      $config['cur_tag_close'] = "&lt;span class='sr-only'&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;";
      $config['next_tag_open'] = "&lt;li&gt;";
      $config['next_tagl_close'] = "&lt;/li&gt;";
      $config['prev_tag_open'] = "&lt;li&gt;";
      $config['prev_tagl_close'] = "&lt;/li&gt;";
      $config['first_tag_open'] = "&lt;li&gt;";
      $config['first_tagl_close'] = "&lt;/li&gt;";
      $config['last_tag_open'] = "&lt;li&gt;";
      $config['last_tagl_close'] = "&lt;/li&gt;";
  
      $this-&gt;pagination-&gt;initialize($config);
  
      $data['users'] = $this-&gt;db-&gt;get('user', $config["per_page"], $this-&gt;uri-&gt;segment(3));
  
      if (isset($this-&gt;request-&gt;post['selected'])) {
          $data['selected'] = (array)$this-&gt;request-&gt;post['selected'];
      } else {
          $data['selected'] = array();
      }
  
      return $this-&gt;load-&gt;view('user/users_list', $data);
  }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>&lt;form action="&lt;?php echo $delete; ?&gt;" method="post" enctype="multipart/form-data" id="form-user"&gt;
  
  &lt;div class="table-responsive"&gt;
  &lt;table class="table table-bordered table-hover"&gt;
  &lt;thead&gt;
  &lt;tr&gt;
  &lt;td style="width: 1px;" class="text-center"&gt;&lt;input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /&gt;&lt;/td&gt;
  &lt;td class="text-center" style="color: #1E91CF; font-size: 14px; font-weight: bold;"&gt;&lt;?php echo $column_user_id; ?&gt;&lt;/td&gt;
  &lt;td class="text-center" style="color: #1E91CF; font-size: 14px; font-weight: bold;"&gt;&lt;?php echo $column_name; ?&gt;&lt;/td&gt;
  &lt;td class="text-center" style="color: #1E91CF; font-size: 14px; font-weight: bold;"&gt;&lt;?php echo $column_status; ?&gt;&lt;/td&gt;
  &lt;td class="text-center" style="color: #1E91CF; font-size: 14px; font-weight: bold;"&gt;&lt;?php echo $column_last_updated; ?&gt;&lt;/td&gt;
  &lt;td class="text-center" style="color: #1E91CF; font-size: 14px; font-weight: bold;"&gt;&lt;?php echo $column_date_added; ?&gt;&lt;/td&gt;
  &lt;td class="text-center" style="color: #1E91CF; font-size: 14px; font-weight: bold;"&gt;&lt;?php echo $column_action; ?&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
  &lt;?php foreach ($users-&gt;result() as $user) { ?&gt;  
  &lt;td class="text-center"&gt;&lt;?php if (in_array($user-&gt;user_id, $selected)) { ?&gt;
  &lt;input type="checkbox" name="selected[]" value="&lt;?php echo $user-&gt;user_id; ?&gt;" checked="checked" /&gt;
  &lt;?php } else { ?&gt;
  &lt;input type="checkbox" name="selected[]" value="&lt;?php echo $user-&gt;user_id ?&gt;" /&gt;
  &lt;?php } ?&gt;
  &lt;/td&gt;
  &lt;td class="text-center"&gt;&lt;?php echo $user-&gt;user_id; ?&gt;&lt;/td&gt;
  &lt;td class="text-center"&gt;&lt;?php echo $user-&gt;username; ?&gt;&lt;/td&gt;
  &lt;td class="text-center"&gt;&lt;?php if ($user-&gt;status == TRUE) { echo $text_enabled; } else { echo $text_disabled ; } ?&gt;&lt;/td&gt;
  &lt;td class="text-center"&gt;&lt;?php echo $user-&gt;last_updated; ?&gt;&lt;/td&gt;
  &lt;td class="text-center"&gt;&lt;?php echo $user-&gt;date_added; ?&gt;&lt;/td&gt;
  &lt;td class="text-center"&gt;&lt;?php echo anchor("admin/users/edit/" . $user-&gt;user_id, '&lt;div class="btn btn-primary text-right" role="button"&gt;&lt;i class="fa fa-pencil"&gt;&lt;/i&gt;
   Edit&lt;/div&gt;');?&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php } ?&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  &lt;div class="row"&gt;
  &lt;div class="col-sm-6 text-left"&gt;
  &lt;?php echo $this-&gt;pagination-&gt;create_links();?&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Model get users</p>
  
  <pre><code>public function getUsers() {
    $this-&gt;db-&gt;select('*');
    $this-&gt;db-&gt;from($this-&gt;db-&gt;dbprefix . 'user');
    $query = $this-&gt;db-&gt;get();
  
    if ($query-&gt;num_rows() &gt; 0) {
        return $query-&gt;result();
        return true;
    } else {
        return false;
    }
  }
  </code></pre>