Welcome Guest, Not a member yet? Register   Sign In
Sortable lists using PHP
#1

[eluser]tim1965[/eluser]
Hi

I am trying to create a list of images that allows the user to set the order in which they are displayed.
So i would display a list of say 5 images and add two links to each image one to move the image up the list and the other to move the image down the list.
So my list would be
Quote:[1] image 1
[2] image 2
[3] image 3
[4] image 4
[5] image 5
So for image 4 if i hit the up link the list would become
Quote:[1] image 1
[2] image 2
[3] image 4
[4] image 3
[5] image 5

I would then save the new order into the db.
I am struggling to even come up with an approach on how to acheive this so i dont have any code currently.
I know i can do this using Javascript. But would prefer to handle this in PHP. So wanted to throw this out to see if anybody else has had to deal with a similar situation.
I hope the above is clear but let know if it isnt.
Thanks in advance for any contributions.
#2

[eluser]rogierb[/eluser]
well, one not so beautiful approach

Add a field to your tabel called 'order' to define the order.
Create a form for each up/down arrow. In this form, create 3 hidden fields
1: record_id
2: order + or -
3: upper/lower record_id depending on up/down arrow


On submit, update your record id with the new order,
Then update the other records (upper or lower) with their new order.

It is far more easy to do by javascript thought
#3

[eluser]tim1965[/eluser]
rogierb

Thanks for the reply.
I am being stupid, i dont understand how that would work, sorry. Could you elaborate please.
Thanks
#4

[eluser]rogierb[/eluser]
Ook, here goes.

Example: arrow down, image[3]

Code:
echo form_open("to the save function");
echo form_hidden("current_record_id" $current_rec_id);
echo form_hidden("other_record_id" $next_rec_id);
echo form hidden("new_order", $old_order + 1);
echo form hidden("ordering", "plus");
echo form_submit("arrow_down","arrow_down");
echo form_close();

Example: arrow up, image[3]

Code:
echo form_open("to the save function");
echo form_hidden("current_record_id" $current_rec_id);
echo form_hidden("other_record_id" $next_rec_id);
echo form hidden("new_order", $old_order - 1);
echo form hidden("ordering", "minus");
echo form_submit("arrow_up","arrow_up");
echo form_close();


Now update both current image and the one that is moved aswell.
So img[3] will have order 2 and img[2] will have order 3 moving img[3] up.
Code:
function save_function()
{
  //update current image
  $this->db->where("id",$this->input->post("current_record_id"))
  $this->db->update("image_table", array("order"=>$this->input->post("order")));

  //update other record
  "plus" = $this->input->post("ordering") ? $other_order =  $this->input->post("order") - 1 : $other_order =  $this->input->post("order") +1;
  
  $this->db->where("id",$this->input->post("other_record_id"))
  $this->db->update("image_table", array("order"=>$other_order));
}

After saving, reload the page,
#5

[eluser]tim1965[/eluser]
Got it. Thanks for the detailed response. I have to go out now, but will try this when i get back.




Theme © iAndrew 2016 - Forum software by © MyBB