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 7599013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:33:16+00:00 2026-05-30T22:33:16+00:00

if I have three menus id | name | order ——————– 1 | Foo

  • 0

if I have three menus

id | name | order
--------------------
1  | Foo  | 1
2  | Bar  | 2
3  | Blah | 3

and click the link menu/up/1, I need to set order of menu with id 1 to 2 and menu with id 2 to 1.

I have tried (PDO)

public static function goDown($id) {
    if(!$id) {
        return false;
    }

    self::connect();
    self::prepare('SELECT count(*) FROM menu');
    $count = self::fetch();

    if(isset($count['count(*)'])) {
        self::prepare('UPDATE menu SET order = order - 1 WHERE order = :count');
        self::bindValue('count', $count['count(*)']);
        self::execute();

        self::prepare('UPDATE menu SET order = order + 1 WHERE id = :id AND order < :count');
        self::bindValue('id', $id);
        self::bindValue('count', $count['count(*)']);

        return self::execute();
    }
}

Thanks!

EDIT – MY SOLUTION:

public static function goDown($id) {
    if(!$id) {
        return false;
    }

    self::connect();
    self::prepare('SELECT count(*) FROM menu');
    $count = self::fetch();

    if(isset($count['count(*)'])) {
        self::prepare('SELECT ordem FROM menu WHERE id = :id LIMIT 1');
        self::bindValue('id', $id);
        $ordem = self::fetch();

        self::prepare('UPDATE menu SET ordem = ordem - 1 WHERE ordem = :ordem');
        self::bindValue('ordem', $ordem['ordem'] + 1);
        self::execute();

        self::prepare('UPDATE menu SET ordem = ordem + 1 WHERE id = :id AND ordem < :count');
        self::bindValue('id', $id);
        self::bindValue('count', $count['count(*)']);

        return self::execute();
    }
}
  • 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-30T22:33:17+00:00Added an answer on May 30, 2026 at 10:33 pm

    I don’t know how your table definition and keys are set up, but you will probably have to have an overlapping number for a split second. If that is ok, then do something like this:

    The safest:

    SELECT order FROM menu WHERE id = :id; /* One to go up; */
    SELECT id, order FROM menu WHERE order = :existingOrder + 1; /* One to go down */
    
    UPDATE menu SET order = order + 1 WHERE id = :id; /* Putting it up */
    UPDATE menu SET order = :existingOrder WHERE id = :existingId
    

    This uses an extra query, but is much safer. You can also validate that it isn’t at the top/bottom, too as that will cause problems.

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

Sidebar

Related Questions

I have three tables menus +--id---ident---+ |--1----menu_1--| menus_data +--id---id_parent---name---------id_lang--+ |--1----1-----------menu_eng------1-------| |--2----1-----------menu_rus------2-------| +--3----1-----------menu_arm------3-------+ languages +--id---name--------+
I have a ASP.Net Menu Control with three levels and flyouts enabled. I want
I have an Array named Menus. It contains a form name per element. How
I have defined some menus in sitemap: ... Menu(index,S ? home) / index ...
I have three tables set up in a MySQL database called event, status and
I am implementing the superfish menu and have noticed that there is a funny
Is there a way I can have a custom quick launch menu on a
There are lots of java apps on my simulator menu screen which I have
I have an ascx component, that holds two-level menu, because there are several user
Hi I have created a java application. I found there is an application menu

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.