Welcome Guest, Not a member yet? Register   Sign In
Change order of records - How's it look??
#1

[eluser]weetstraw[/eluser]
I wanted to give my client the ability to reorder a list of records and I thought the simplest way was to move a record up one spot at a time and have the record above move down one. Each row has a field called "p_order". When a new record is inserted, a query is made to determine the highest order number, adds one and inserts it.

When you click the "up" link as show in the view below:
- The current value of "p_order" is given to the record above.
- The current value of "p_order" has 1 added to it and updated for the current record.

How's it look? I figured when refined this could be very useful for a lot of people.

The View - for now, just a link that says "up"
Code:
<?php echo anchor("manage/moveUp/$row->p_order/$row->p_cat/$row->p_id",'up'); ?>

The Controller
Code:
function moveUp($order,$cat,$id)
{
    $order=$this->uri->segment(3);
    $cat=$this->uri->segment(4);
    $id=$this->uri->segment(5);

    $this->Manage_model->moveUp($order,$cat,$id);
}

The Model
Code:
function moveUp($order,$cat,$id)
{            
    $up=$order+1;
    
    $this->db->where('p_order',$up);
    $this->db->where('p_cat',$cat);
    $this->db->update('portfolio',array('p_order'=>$order));
    
    $this->db->where('p_id',$id);
    $this->db->where('p_cat',$cat);
    $this->db->update('portfolio',array('p_order'=>$up));    
}

In addition, here is the controller to add a new record, notice the first query figures out the order:
Code:
/* Insert Record Action*/

function insertRecord()
{
    $this->db->select_max('p_order');
    $query=$this->db->get('portfolio');
    
    $row=$query->row();
    
    $order=$row->p_order;
    
    $this->Manage_model->insertRecord($order);


    redirect('manage');
}
#2

[eluser]slowgary[/eluser]
The only issue with this method is if the list of records is very long, it's a real pain in the butt. Especially if you're moving a record from the bottom to the top. Also, what if your records are paginated?

You could alternatively use something like jQueryUI, which has a 'sortables' function which will turn a set of elements into a sortable list, drag and drop style. The only downside is that saving the changes would either require you to update all rows or save a second copy for comparison to know which rows were changed.
#3

[eluser]weetstraw[/eluser]
Agreed but this is for around twenty records so I think it's pretty manageable.




Theme © iAndrew 2016 - Forum software by © MyBB