Jump to content

Reorder MySQL rows via PHP?

  • Please log in to reply
2 replies to this topic

#1 Guest_convention_*

  • Guests

Posted 29 October 2006 - 05:53 AM

I have a webpage that displays all of the MySQL rows in descending order. Though I don't know if this matters, all rows have a unique id number (auto_increment, primary key). I am wondering, is it possible to reorder any entry in MySQL in PHP (such as a form, GET, or any other method)? For example, move a row from the bottom of the page to, say somewhere in the middle?

I don't really want to do it by using the 'ORDER BY' SQL statement, but rather actually move the rows in the MySQL database.

Thank you for the help. :)

#2 Psycho

  • Moderators
  • Move along, nothing to see here
  • 11,891 posts
  • LocationCanada

Posted 29 October 2006 - 05:59 AM

Sorry, but that's just stupid. That's the whole purpose of the ORDER BY statement. If you have data that you want ordered in a specific way that cannot be achieved using ORDER BY then you should add a "sort" column to the data and then ORDER BY that.

And, the only way to reprder the rows would be to create a script that would overwrite all the records in the order you want. Again, bad idea.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 .josh

  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 29 October 2006 - 08:13 AM

perhaps you need to be a bit clearer.  Are you for instance, wanting to have a list of stuff that you want to be able to move up and down the list, such as, on a message board, having say 10 forums, and being able to move a forum up or down the list?

if so, then what you will want to do is create another field in your table called "layout_order" or other some such descriptive thing.  Then you will want to in your query, order by layout_order.  If you want to "bump" the item up a notch, then you would swap numbers with the it and the one above it. 
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users