| | |
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
Views: 1386 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery js limit link login loop mail mediawiki menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql stored structure subdomain syntax system table tutorial update updates upload url validation validator variable video web xml youtube





