views:

84

answers:

2

I am using PHP/MYSQL. I want in my frontend, for the users to sort and rearrange database records entered by the user himself in an earlier stage. The records uploaded by the user may sometimes inserted into the database randomly, so in the user profile he/she might have a facility to rearrange the the rows in the database according to there wise.

Can any one provide me a script that would help me to do so.

id  title     date     desc
1   s1        s1_date  s1_desc
2   s2        s2_date  s2_desc
3   s3        s3_date  s3_desc
4   s4        s4_date  s4_desc
5   s5        s5_date  s5_desc

In the user profile he want to rearrange the rows so that s2 will go one up and will save as s2->s1 and s1->s2 for every field.. may be swapping of records in between

I am looking it in another way.. user page will show the field in rows wise and in the side there will be an input box of each the current row id say (id) is set as value of that input box so that the user can change in between and submit the whole reordered list to the back-end. In that case what can we do... there will be many swapings to to concurrently..

+1  A: 

Best solution is to add another field "ordering" where you can store by default int ID, and while rearranging rows, you could swap values of ordering between these rows. In query you would do ORDER BY ordering ASC/DESC

cichy
+1  A: 

Get both rows and update back the values:

Assuming the tablename is: t

$result = mysql_query("SELECT title, `date`, `desc` FROM t WHERE id = 1");
$row1 = mysql_fetch_assoc($result);

$result = mysql_query("SELECT title, `date`, `desc` FROM t WHERE id = 2");
$row2 = mysql_fetch_assoc($result);

mysql_query("UPDATE t SET title = '{$row2['title']}', `date` = '{$row2['date']}', `desc` = '{$row2['desc']}' WHERE id = 1");
mysql_query("UPDATE t SET title = '{$row1['title']}', `date` = '{$row1['date']}', `desc` = '{$row1['desc']}' WHERE id = 2");
shamittomar
thanks for you answer. I like your script..I am looking it in another way.. user page will show the field in rows wise and in the side here will be an input box of each the current row id say (`id`) is set as value of that input box so that the user can change in between and submit the whole reordered list to the back-end. In that case what can we do... there will be many swapings to to concurrently...is there any idea..problem also may occur while we apply the above code in loop in this case...isnt it
Rahul TS
OK, can I know why do you have such low **answer accept rate** and very less voting ? You do not seem to accept answers or vote on them. Probably this is the reason you get very few answers / comments. So, on STackOverflow, please accept answers which work out for you and always vote on answers you like.
shamittomar
yes sure, I wise to accept good answers..As I am a newbie to this I dont know much of its dos and dont. can you answer me on the above task
Rahul TS