View Full Version : reorder links

11-04-2010, 12:49 AM
I am working on a site where a user can post links to different sites.. I need a way to let users reorder these links that they add... right now if a user wants to reorder their links they have to delete them all and add them on in the order they want them to be in... which obviously isn't very good.. so I need a new way to let users reorder links

I would like to let users reorder them using a drag and drop interface, but I don't know how to use ajax to make the php calls to save the new spot of the link... I would also like to make sure that users without javascript are taken care of too.. I think the best way to do this is do have a column on the database called ordernumber or something like that... and the order number would change depending on the spot, but I would have to change the order number on everything else.... Any Ideas/suggestions?

11-05-2010, 12:47 AM
Without using Javascript/AJAX the method is a bit clumsy, but works fine. As you mentioned, store a "sort order" integer on each row in your database and then provide "up" and "down" buttons for your users on each and every row of data on your page. Make the buttons so they send the unique ID of the data to your PHP script.

When the "up" button is pressed, you'll do three queries: The first query gets the sort order number of the row just above the current row, such as SELECT sort_order_nbr, unique_id FROM yourtable WHERE sort_order_nbr < [current sort_order_nr] LIMIT 1. The second query updates this row you just selected with the sort_order_nbr of the current row. The third query updates the current row with the sort_order_nbr from the previous row.

In other words, you simply SWAP the sort order numbers. Do the same for the down button, only swap sort order with the next row in line.