neo-claw Posted February 2, 2008 Share Posted February 2, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/89107-order-menu-items/ Share on other sites More sharing options...
p2grace Posted February 2, 2008 Share Posted February 2, 2008 Basically the orderNumber would be the same as the incrementing id when first being added. When you want to switch you'd simply swap the two orderNumbers. Quote Link to comment https://forums.phpfreaks.com/topic/89107-order-menu-items/#findComment-456371 Share on other sites More sharing options...
neo-claw Posted February 2, 2008 Author Share Posted February 2, 2008 ehm.. don't get it :-\ Quote Link to comment https://forums.phpfreaks.com/topic/89107-order-menu-items/#findComment-456374 Share on other sites More sharing options...
p2grace Posted February 2, 2008 Share Posted February 2, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89107-order-menu-items/#findComment-456383 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.