[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');
}