电子商务网站上的嵌套模型菜单

发布于 2024-12-03 20:33:32 字数 1029 浏览 0 评论 0原文

我有一个带有类别表的数据库,使用 MySql 中的嵌套方法。但我总是碰壁。该网站包含“部分”物种(因为这是一家宠物商店)。我已经有一个查询,该查询返回包含每个物种的产品的所有类别。

现在我的问题是我希望能够只显示包含产品的类别。例如,如果我使用儿童成人创建类别food。那么系统不应该在任何物种下列出该类别。但是,一旦我在 adult 类别下创建一个 Product,系统就会列出 Food 以及子 Adult

该表目前的结构如下:

Category:

  • id
  • top
  • name
  • lft
  • rgt
  • 创建的(为了完整性)
  • lastupdated (为了完整性)

所以我能够从表中为每个物种构建一个完整的树,但我需要类别只有在包含该物种的产品后才可见。

听起来很简单,但由于某种原因,我无法理解这一点。有帮助吗?解决这个问题的最佳方法是什么?

更新:

我用来获取类别的查询:

SELECT node.id as nid, node.top as top, node.*, brands.name as brandname from
brands, foods, foodcategories, categories as node, categories as parent WHERE 
node.lft BETWEEN parent.lft AND parent.rgt AND parent.top=1 AND 
foodcategories.food=foods.id AND foodcategories.category=node.id 
AND brands.id=foods.brand AND foods.species={speciesid}

然后我执行一个查询来获取所有类别及其 lft、rgt。我可以用它来建造树。

I have a Database with a category table using the Nested Approach in MySql. But I keep hitting a wall. The Site contains "sections" species (As this is a pet store). I already have a query that returns all the categories that have products in them for each species.

Now my problem is that I want to be able to only show categories that have products under them. So for example if I create the category food with a child adult. Then the system should not list the category under any species. But as soon as I create a Product under the adult category the system should list Food with the child Adult .

The Table is currently structured as follows:

Category:

  • id
  • top
  • name
  • lft
  • rgt
  • created (for the sake of completeness)
  • lastupdated (for the sake of completeness)

So I am able to build a full tree from the table for each species but I need the categories to only be visible once they have products in them for the species.

Sounds simple enough but for some reason I can't get my brain around this. Any Help? What would be the best way to approach this?

Update:

The query I'm using to get the Categories:

SELECT node.id as nid, node.top as top, node.*, brands.name as brandname from
brands, foods, foodcategories, categories as node, categories as parent WHERE 
node.lft BETWEEN parent.lft AND parent.rgt AND parent.top=1 AND 
foodcategories.food=foods.id AND foodcategories.category=node.id 
AND brands.id=foods.brand AND foods.species={speciesid}

I then execute a query to get all the categories and their lft,rgt. Which I can built the tree from.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

治碍 2024-12-10 20:33:32

像这样的东西应该可以工作:

$q = mysql_query("SELECT a.* FROM Categories a WHERE a.id IN (SELECT categoryID FROM Products)");

或者

$q = mysql_query("SELECT a.* FROM Categories a WHERE (SELECT count(*) FROM Products WHERE categoryID = a.id) >= 1");  

您将需要匹配您的字段并将其余代码写入输出,但这应该可以帮助您开始。您还需要更改 a.* 以列出用于更好优化的所有字段。

编辑:

我理解你想要完成的任务,但仍然不理解你的数据库结构。对于 1 级子类别,我会这样做:

echo '<ul>';
$q = mysql_query("SELECT a.* FROM Categories a, SubCategories b WHERE a.ID = b.categoryID AND (SELECT count(*) FROM Products WHERE SubCategoryID = b.id) >= 1");
while($f = mysql_fetch_array($q)) {
    echo '<li>'.$f['Category'];
    $qsc = mysql_query("SELECT b.* FROM SubCategories b WHERE (SELECT count(*) FROM Products WHERE SubCategoryID = b.id) >= 1");
    if (mysql_num_rows($qsc) > 0) {
        echo '<ul>';
    }
    while($fsc = mysql_fetch_array($fsc)) {
        echo '<li>'.$fsc['SubCategory'].'</li>';
    }
    if (mysql_num_rows($qsc) > 0) {
        echo '</ul>';
    }   
    echo '</li>';   
}
echo '</ul>';

您可以使用它并适应您的代码,或者如果您进一步解释一下您的数据库结构,我可以提供进一步的帮助。

Something like this should work:

$q = mysql_query("SELECT a.* FROM Categories a WHERE a.id IN (SELECT categoryID FROM Products)");

OR

$q = mysql_query("SELECT a.* FROM Categories a WHERE (SELECT count(*) FROM Products WHERE categoryID = a.id) >= 1");  

You will need to match up your fields and write the rest of the code to output, but this should get you started. You also will want to change a.* to list all your fields being used for better optimization.

EDIT:

I understand what you are trying to accomplish I think, but still don't understand your database structure. For 1 level of subcategories, I would do something like:

echo '<ul>';
$q = mysql_query("SELECT a.* FROM Categories a, SubCategories b WHERE a.ID = b.categoryID AND (SELECT count(*) FROM Products WHERE SubCategoryID = b.id) >= 1");
while($f = mysql_fetch_array($q)) {
    echo '<li>'.$f['Category'];
    $qsc = mysql_query("SELECT b.* FROM SubCategories b WHERE (SELECT count(*) FROM Products WHERE SubCategoryID = b.id) >= 1");
    if (mysql_num_rows($qsc) > 0) {
        echo '<ul>';
    }
    while($fsc = mysql_fetch_array($fsc)) {
        echo '<li>'.$fsc['SubCategory'].'</li>';
    }
    if (mysql_num_rows($qsc) > 0) {
        echo '</ul>';
    }   
    echo '</li>';   
}
echo '</ul>';

You can take this and adapt to your code or if you explain a little more about your database structure, I can help further.

难以启齿的温柔 2024-12-10 20:33:32

经过几天的努力,终于想到了一个解决方案并实施了。

我可以执行以下查询并获取进出类别的完整路径,

(SELECT cast(group_concat(parent.id SEPARATOR '-') AS char) as catpath
            FROM categories AS node,
                    categories AS parent
            WHERE node.lft BETWEEN parent.lft AND parent.rgt
                    AND node.id=categories.id
            ORDER BY node.lft )

然后我想如果可以的话为什么不在类别查询中包含此路径。因此,当我检索包含产品的类别时,我可以简单地解析该字符串并将 id 与我也检索到的类别列表相匹配。

这有点循环。所以我缓存了菜单并且每 5 分钟只生成一次。

就是这样! 3 条查询!

  • 其中之一是物种清单。
  • 一个适用于所有类别
    其中的产品。
  • 一个适用于我循环查找的所有类别
    具有一定的ID。

迎接下一个挑战!

After a couple of days struggling a finally thought of a solution and implemented it.

I can execute the following query and get the entire path to and from a category,

(SELECT cast(group_concat(parent.id SEPARATOR '-') AS char) as catpath
            FROM categories AS node,
                    categories AS parent
            WHERE node.lft BETWEEN parent.lft AND parent.rgt
                    AND node.id=categories.id
            ORDER BY node.lft )

then I thought if I could this why not include this path in the category query. So when I retrieve the categories that have products in them I can simply parse this string and match the id to a list of categories that I also retrieve.

This is quite a bit of looping. So I cache the menu and only generate it once every 5 min.

So that's it! 3 Queries!

  • One for the list of species.
  • One for all the categories that have
    products in them.
  • One for all the categories that I loop to find one
    with a certain ID.

On to the next challenge !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文