| | |
How to swap two rows of a table in a database using php
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Hello I have designed a website in which users can create their account and then add or delete their likings, favoorite songs, what i want is that i want them to be able to arrange their likings by moving up and down. for this i need a procedure for swapping the two rows of table so that they can arrange their liking and songs. Please help me, if you have any other idea please share with me.
Thanks in advance.
Thanks in advance.
If you think we fight for money and you fight for honor, then remember everyone fights for the thing they don't have...
•
•
•
•
Hi,
The easiest way that I can think of is:
Create the table dynamically and then when you move up and down, swap the table rows using javascript.
Addionitally you're going to need a column in your database named "Order" or something. I assume that your "Likings" table has the columns song_id, user_id. Add to that an order column and when they move the songs to different positions, just update the row.
If your database is arranged differently, you can still use my method. Just add an order column to your table.
Last edited by MVied; Aug 18th, 2008 at 12:53 pm.
One of the things that I have done in the past(some would prefer not to because it will change the primary keys) is to delete the values in the table pertaining to that user and then add the values as if it was the first time. This will readjust the order naturally and save the overhead but your primary keys will get high, but big deal right? This option will only work if you don't have foreign keys referencing these primary keys since the primary keys will change.
Last edited by R0bb0b; Aug 18th, 2008 at 2:51 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
![]() |
Other Threads in the PHP Forum
- Previous Thread: Echo MySQL Row Data
- Next Thread: How to use getimagesize
| Thread Tools | Search this Thread |
.htaccess ajax apache api array beginner binary broken cakephp checkbox class cms code compression cron curl data database date directory display download dynamic echo email error file files folder foreach form forms function functions google href htaccess html httppost image include insert integration ip java javascript joomla limit link links login loop mail md5 menu mlm mod_rewrite multiple mysql mysql_real_escape_string oop paypal pdf php problem query radio random recursion regex remote script search searchbox server session sessions sms soap source space specific sql syntax system table tutorial update upload url validation validator variable video volume votedown web website xml youtube





