943,513 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2356
  • PHP RSS
Aug 18th, 2008
0

How to swap two rows of a table in a database using php

Expand Post »
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.
Reputation Points: 40
Solved Threads: 7
Junior Poster
architact is offline Offline
114 posts
since Apr 2008
Aug 18th, 2008
1

Re: How to swap two rows of a table in a database using php

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.
Reputation Points: 28
Solved Threads: 19
Junior Poster
vicky_rawat is offline Offline
137 posts
since Jun 2008
Aug 18th, 2008
1

Re: How to swap two rows of a table in a database using php

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.
Certainly the fancy way would be to use something like the Mootools Sortables class to sort a list. You could combine that with some type of server-side language to have it update when they click an Apply button, or even have it update the database every time they move something.

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.
Reputation Points: 21
Solved Threads: 11
Junior Poster
MVied is offline Offline
111 posts
since Aug 2008
Aug 18th, 2008
0

Re: How to swap two rows of a table in a database using php

thanks alot MVied and vicky i will try your methods. thanks
Reputation Points: 40
Solved Threads: 7
Junior Poster
architact is offline Offline
114 posts
since Apr 2008
Aug 18th, 2008
0

Re: How to swap two rows of a table in a database using php

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.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Echo MySQL Row Data
Next Thread in PHP Forum Timeline: How to use getimagesize





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC