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

  • SEARCH
  • Home
  • 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 7872781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:18:48+00:00 2026-06-03T02:18:48+00:00

I arrange photos on an album by using a column sort in the table.

  • 0

I arrange photos on an album by using a column “sort” in the table.
This means that an album will be displayed with “ORDER BY sort”.

I have a feature that allows users to arrange the photos on an album, by dragging and dropping photos, in javascript, and then pressing the “Save” button.

An array (with the sorted Photo IDs) is sent to the process (in PHP), and then I want to re-sort the rows on the table.

The easiest way in my mind to do is:

for ($c=0; $c<$length; $c++) {
    mysql_query('UPDATE photos SET sort="'.$c.'" WHERE id="'.$array[$c].'"');
}

(please ignore sanitization, duplicates and other verifications here)

But I’m worried about the amount of queries that are made in a cycle like this.

How would you improve this approach?

Thank you.

  • 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-06-03T02:18:49+00:00Added an answer on June 3, 2026 at 2:18 am

    First of all, I don’t think there’s anything wrong with issuing multiple queries. Did you test it? Are you sure it would be a performance bottleneck? I don’t think so.

    But anyway, solution #1 is to use REPLACE in conjunction with the VALUES clause. In that case, though, you need to explicitly specify data for all columns of the table, otherwise the data in those columns will be erased (set to default value).

    Solution #2 is just for fun. I don’t think anyone would use it, but still:

    UPDATE photos
    SET sort = (
      SELECT sort FROM (
        SELECT 1 id, 1 sort UNION
        SELECT 2 id, 2 sort UNION
        SELECT 123 id, 3 sort) t
      WHERE t.id = photos.id)
    WHERE id IN (1, 2, 123)
    

    Solution #3 is virtually the same as Zombaya’s:

    UPDATE photos
    SET sort =
      IF(id = 1, 1,
      IF(id = 2, 2,
      IF(id = 123, 3,
        0)))
    WHERE id IN (1, 2, 123)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this php code who auto thumb and arrange my photos to gallery:
How can I arrange students in descending order? Here is my code. using System;
I'd like to arrange things so that I have a chain of keyboard event
i see there is arrange tables that organizes your table relationships to be clear.
in navicat you can arrange the order of the columns but i have yet
Using the Arrange Act Assert what should be in the Arrange section considering that
Given a collection that I want to arrange on a page like this: <!--
My problems is that I have to arrange, when searching for a customer, arrange
I'm thinking how to arrange a deployed python application which will have a Executable
I need to arrange the nodes in XHTML in particular predefined order using XSL,

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.