开发者

Riddle me this. PHP brain twister, sortable, editable table

I have a table that is editable and sortable. The table has 2 type types of items parents and children. I would like the user to be able to switch between the two and drag between the two, while updating the order. The database table is similar to the following.

+-------------------------------------------------------+-----------+
|id | userid  | item            | parent | item order   | parent_id |
+-------------------------------------------------------+-----------+
|23 | 5       | cheese burger   |false   |     2        |    128    |
+-------------------------------------------------------+-----------+
|128| 5       | Burgers         |True    |     0        |    0      |
+-------------------------------------------------------+-----------+
|10 | 5       | Hamburger       |false   |     1        |    128    |
+-------------------------------------------------------+-----------+
|25 | 5       | steak burger    |false   |     3        |    128    |
+-------------------------------------------------------+-----------+
|90 | 5       | fish   burger   |false   |     4        |    128    |
+-------------------------------------------------------+-----------+
|29 | 5       | fries           |True    |     5        |     0     |
+-------------------------------------------------------+-----------+
|22 | 5       | cheese Fries    |false   |     6        |     29    |
+-------------------------------------------------------+-----------+
|987| 5       | french fries    |false   |     7        |     29    |
+-------------------------------------------------------+-----------+

This will give me a menu like so....

BURGERS
Hamburger
Cheese burger
Steak burger
Fish burger
FRIES
Cheese fries
French fries

Upper case denoting a header*

I can get the order correct, no problem. My script POST's an array of the item order.

 Array
 (
   [0] => 128
   [1] => 10
   [2] => 23
   [3] => 25
   [4] => 90
   [5] => 29
   [6] => 22
   [7] => 987
)

Then I update the DB to reflect this, similar to this...

 UPDATE menu SET item_order = '0' WHERE item = '128'
 UPDATE menu SET item_order = '1' WHERE item = '10'
 UPDATE menu SET item_order = '2' WHERE item = '23'
 UPDATE menu SET item_order = '3' WHERE item = '25'
 UPDATE menu SET item_order = '4' WHERE item = '90'
 UPDATE menu SET item_order = '5' WHERE item = '29'
 UPDATE menu SET item_order = '6' WHERE item = '22'
 UPDATE menu SET item_order = '7' WHERE item = '987'

where I am stumbling is getting the parent id right. I have tried all sorts of crazy methods , finding the parent ID then doing a < > sort of thing but nothing seems to get it consistently right. You could drag a header around and this will screw up the children. i.e all the children will adopt the dragged header. How would you approach this? The above update happens when ever a new item is added or an item is dragged.

EDIT

This seems to work......

 $order = "SELECT itemOrder, itemId, header from menuItems && user = $user order by itemOrder"; 
    $order_query = mysql_query($order);
    while($order_result = mysql_fetch_array($order_query))
        {
        $parent = $order_res开发者_如何学Pythonult['header'];
        if ($parent == 'true')
            {
            $current_parent = $order_result['itemId'];  
            }   
            $item_id = $order_result['item_id'];
            $parent_update = "UPDATE menuItems SET parent = '$current_parent' WHERE itemId = $itemId";
          mysql_query($parent_update);


Since a menu will probably not be a hierarchical structure, I wonder if it wouldn't be easier to have two tables, one containing the headers and the other containing the items.

It won't matter much for this solution, though.

About the parentid:

Assuming each item that isn't a parent itself should have a parent, you could just loop through all items in their posted order and apply this (pseudo code) logic:

$parentId = null;

loop through items:
  if item->type == header:
     set $parentId to item->id
     update sort order
  else (item is.. well, item)
     check if $parentId !== null. Fail otherwise (item above first header)
     update sort order and parentId
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜