Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6173681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:37:08+00:00 2026-05-23T23:37:08+00:00

Assume PHP/MYSQL but I don’t necessarily need actual code, I’m just interested in the

  • 0

Assume PHP/MYSQL but I don’t necessarily need actual code, I’m just interested in the theory behind it.

A good use-case would be Facebook’s photo gallery page. You can drag and drop a photo on the page, which fires an Ajax event to save the new sort order. I’m implementing something very similar.

For example, I have a database table “photos” with about a million records:

photos
id : int,
userid : int,
albumid : int,
sortorder : int,
filename : varchar,
title : varchar

Let’s say I have an album with 100 photos. I drag/drop a photo into a new location and the Ajax event fires off to save on the server.

Should I be passing the entire array of photo ids back to the server and updating every record? Assume input validation by “WHERE userid=loggedin_id“, so malicious users can only mess with the sort order of their own photos

Should I be passing the photo id, its previous sortorder index and its new sortorder index, retrieve all records between these 2 indices, sort them, then update their orders?

What happens if there are thousands of photos in a single gallery and the sort order is changed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:37:09+00:00Added an answer on May 23, 2026 at 11:37 pm

    What about just using an integer column which defines the order? By default you assign numbers * 1000, like 1000, 2000, 3000…. and if you move 3000 between 1000 and 2000 you change it to 1500. So in most cases you don’t need to update the other numbers at all. I use this approach and it works well. You could also use double but then you don’t have control about the precision and rounding errors, so rather don’t use it.

    So the algorithm would look like: say you move B to position after A. First perform select to see the order of the record next to A. If it is at least +2 higher than the order of A then you just set order of B to fit in between. But if it’s just +1 higher (there is no space after A), you select the bordering records of B to see how much space is on this side, divide by 2 and then add this value to the order of all the records between A and B. That’s it!

    (Note that you should use transaction/locking for any algorithm which contains more than a single query, so this applies to this case too. The easiest way is to use InnoDB transaction.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(SOLVED, SEE END) I need hints on the PHP or MySQL code structure for
My dev team uses WAMP for the php/Mysql environment but i just learnt that
I don't know why but the code below is working when I have a
I have a bit of PHP code which I need to return an even
Disclaimer: I'm familiar with PHP, MySQL, jQuery, AJAX, but am by no means an
(assume php5) consider <?php $foo = 'some words'; //case 1 print these are $foo;
Let's assume we have the following structure: index.php config.inc.php \ lib \ lib \
Let's assume I'm developing a AJAX, PHP chess game. During the game, one movement
I have a question regarding static function in php. let's assume that I have
i would like to learn how to make PHP/CSS syntax generator. i assume this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.