CodeIgniter Forums
Is there away I can order table data ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Is there away I can order table data ? (/showthread.php?tid=71940)



Is there away I can order table data ? - cyjuice - 10-15-2018

[Image: hdVdw.png]

My table looks like this , and I'm trying to allow user to change data order asc/desc by clicking the button at thead. I'm not sure how to do it , any idea ?


RE: Is there away I can order table data ? - php_rocs - 10-15-2018

@cyjuice,

I'm unable to see the image. There are multiple things you can do to achieve your objective (Datatables [ https://datatables.net/ ] is one suggestion). Hopefully you can add a picture so that we can see what we are working with.


RE: Is there away I can order table data ? - Wouter60 - 10-15-2018

Without any code from your part, it's just guessing how you initialize the table, but let's assume you've read the CI documentation.

Your controller function could look like this (controller named "Orders.php"):
PHP Code:
public function table($order 'asc')
{
 
   $data['records'] = $this->orders_model->get_all($order);  //get records from model
 
   $data['change_order'] = $order == 'asc' 'desc' 'asc'  //set value for changing the order
 
   //load view to show the table records:
 
   $this->load->view('header');
 
   $this->load->view('orders_table',$data);
 
   $this->load->view('footer');


Your model (Orders_model.php):
PHP Code:
public function get_all($order)
{
 
  $query $this->db->order_by('order_date'$order)->get('orders');
 
  if ($query->num_rows() > 0) {
 
     return $query->result();
 
  
 
  else {
 
     return NULL;
 
  }


Your view (orders_table.php):
PHP Code:
<table>
 
 <thead>
 
   <tr><th>No</th><th>ID</td><th>Data</th><th>Order Date <?= anchor('orders/table/' $change_orderChange Order);?></th></tr>
  </thead>
  <tbody>
   ….
  </tbody>
</table> 

In this example, you won't see a button, but just a link to change the current order from 'asc' to 'desc'. Hopefully this will give you the idea how it works. Later on, you can change the link into a button.

Good luck!