Jump to content

Order menu items


neo-claw

Recommended Posts

I got a problem with my menu ordering in my admin system at my site.

I want to use a up and down arrow to move items up and down, but I need help with the functions.

The menu items in my database has their own unique id, and an ordering number. Any who can help me write a function that can move my items up and down?

Link to comment
Share on other sites

Alright to start, here's your db table.

 

users

uid*

orderNum

username

timestamp

 

Your insert query would look like this:

<?php
$query = "INSERT INTO `users` (`uid`,`username`) VALUES ('','$username')";
$run = mysql_query($query);
$id = mysql_insert_id();
$query = "UPDATE `users` SET `orderNum` = '$id' WHERE `uid` = '$id'";
$run = mysql_query($query);
?>

 

Now you have the user in the database.  To swap the order numbers you'd do this.

 

<?php
// Assuming you have the id of the one you want to move.
$query = "SELECT `orderNumber` FROM `users` WHERE `id` = '$id'";
$run = mysql_query($query);
$orderNum = mysql_result($run,"0","orderNumber");

$direction = ">";
// Now grab id and orderNumber of the next person.  Assuming the direction you want to swap with is provided
$query = "SELECT `id` AS `update_id`,`orderNumber` AS `update_orderNumber` FROM `users` WHERE `orderNumber` $direction '$orderNum' LIMIT 0,1";
$run = mysql_query($query);
$arr = mysql_fetch_assoc($run);
extract($arr);
// Now run the update queries to both.
$query = "UPDATE `users` SET `orderNumber` = '$update_orderNumber' WHERE `id` = '$id'";
$run = mysql_query($query);
$query = "UPDATE `users` SET `orderNumber` = '$orderNum' WHERE `id` = '$update_id'";
$run = mysql_query($query);

// Now the two items should have successfully swapped order numbers.
?>

 

This is obviously a basic non-tested example.  But the idea is there.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.