0

I know that there are currently questions on SO already pertaining to saving jQuery sortable lists to a database. How can I query my 2 tables, grab the "order" string, and then display the sortable list in the correct order. My tables are:

Table name: clothing_category
  id           name              order   
 -----------------------------------------
  5           Headwear          29,22,26

And:

 Table name: clothing_type
  id           name           clothing_id (relational with to 'clothing_category')
 -----------------------------------------
  22           Hat               5
  26           Beanie            5
  29           Visor             5

So, seeing as how the "order" of headwear is stored as `29,22,26', I would want the HTML to output like this:

<ul id="sortable">
   <li id="29">Visor</li>
   <li id="22">Hat</li>
   <li id="26">Beanie</li>
</ul>

How can I display it like above? And is my method of storing the order a good way to do this?

4

1 回答 1

4

我会在clothing_type 表中添加一个Order 或Sequence 列,其中的数字从1 到3(在本例中)。查询表时,按序列字段排序。

要更新,请使用以下伪代码:

$orderedID = array(29,22,26);
foreach($orderedID as $i => $id) {
    $query = 'UPDATE `clothing_type` SET `sequence`=' . $i . ' WHERE `id`=' . intval($id);
}
于 2012-06-25T22:50:35.660 回答