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
https://forums.phpfreaks.com/topic/89107-order-menu-items/
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
https://forums.phpfreaks.com/topic/89107-order-menu-items/#findComment-456383
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.