Welcome Guest, Not a member yet? Register   Sign In
Is there away I can order table data ?
#1

[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 ?
Reply
#2

(This post was last modified: 10-15-2018, 06:42 AM by php_rocs.)

@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.
Reply
#3

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!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB