简体   繁体   中英

PHP/MySQL How to prioritize rows in table via admin panel

I have a custom Wordpress plugin I'm creating. It's a restaurant menu creator. I've got 5 columns in the database ( id, menu_item, meal, menu order ). Meal is (0=Breakfast, 1=Lunch, or 2=Dinner) and menu order an integer to prioritize menu items higher or lower. I've built the admin panel out which reads the following table and displays the items by meal, then by menu order.

    ID  |  MENU ITEM       |  MEAL  |  MENU ORDER
    _____________________________________________  
    1   |  Scrambled Eggs  |  0     |  0 
    6   |  Cereal          |  0     |  1
    5   |  Omelette        |  0     |  2
    2   |  Soup            |  1     |  0
    7   |  Broccoli        |  1     |  1
    4   |  Steak           |  2     |  0
    3   |  Chicken         |  2     |  1

I can add new items just fine by searching for the meal the item is being added to and finding the last menu order number and adding it after. The problem comes in when I want to delete an item. How to reorder the menu order column effectively? Is the table structure set up effectively?

Joomla does this with their articles and modules. You are able to sort priority up and down within the admin panel.

Anyone have any thoughts?

by running a single sql query :)

$deleted_meal_type = 0; //Breakfast- the Type of the Item being deleted
$deleted_meal_order = 7; //The order of the Item being deleted
query("UPDATE meals_table SET menu_order=menu_order-1 WHEN menu_order>$deleted_meal_order  AND meal_type=$deleted_meal_type;"

tada

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM