0

我有一个小问题,我找不到答案。

我有一个名为product_features以下字段的表:

product_features
----------------------
 id  parent_id  name      order    added_date

现在的问题是我刚刚插入了 2 行具有相同名称:

734     1   6008-2 test     0   2012-11-21 11:52:28
735     1   6008-2 test     0   2012-11-21 11:57:27

我已经定义id - PRIMARY INDEX - AUTO INCREMENTparent_id - INDEX

Keyname Type    Unique  Packed  Column     Cardinality  Collation
PRIMARY BTREE   Yes      No product_feature_id  671         A       
product_feature_parent_id   BTREE   No  No  product_feature_parent_id   671 A

当我打印结果时,唯一打印的行是最后一行。我真的不明白为什么。这是我正在使用的功能:

public function printProductFeaturesTable($product_feature_parent_id = 0, $level = 0){
        $sql = $this->mysqli->query("SELECT product_feature_id, product_feature_name
                                     FROM product_features
                                     WHERE product_feature_parent_id='" . $product_feature_parent_id . "'
                                     ORDER BY product_feature_order");
        if($sql->num_rows != 0) {

            if($level != 0)
                echo '<table cellpadding="0" cellspacing="1" class="dnd"><tbody>';

            $i = 0;
            while($row = $sql->fetch_assoc()) {
                $i++;
                echo '<tr>
                        <td width="30" valign="top" align="center">', 
                            $i, '.
                        </td>
                        <td>', 
                            $row['product_feature_name'],
                            '<div class="table-options">
                                <input type="hidden" name="product_feature_id[]" value="', $row['product_feature_id'], '" /> 
                                <a href="/admin/product-features/add-modify/', $row['product_feature_id'], '" class="ui-state-default ui-corner-all left"><span class="ui-icon ui-icon-pencil"></span></a>
                                <a href="javascript:if(confirm(\'Are you sure you want to delete this feature?\')) document.location = \'/admin/resources/php/product-features.php?p=delete&pfid=', $row['product_feature_id'], '\'" class="ui-state-default ui-corner-all left"><span class="ui-icon ui-icon-close"></span></a>
                            </div>';

                $this->printProductFeaturesTable($row['product_feature_id'], $level + 1);

                echo '  </td>
                    </tr>'; 
            }

            if($level != 0)
                echo '</tbody></table>';
        } else
            if($product_feature_parent_id == 0)
                echo '<tr><td align="center" colspan="2">Nici o specificatie de produs adaugata momentan!</td></tr>';
    }

结果应该是这样的:

1. Test
   1. Test subcategory
   2. Test subcategory 2
   3. 6008-2 test
   4. 6008-2 test

但不是上面的例子,结果是:

1. Test
   1. Test subcategory
   2. Test subcategory 2
   3. 6008-2 test // this is the last inserted row.. but where is the other one???

提前谢谢你!

4

1 回答 1

1

Ok.. I've just discovered that the only problem were my eyes, as you can see i'm ordering the results by product_feature_order, which in this example is 0 by default.

So the problem was that the order they are shows is not in the order of the inserted id, which really confused me..

Thank you for your time!

于 2012-11-21T10:41:15.647 回答