带产品计数的递归菜单

发布于 2024-11-19 04:23:47 字数 1676 浏览 4 评论 0原文

我想创建一个包含产品总数的递归菜单,但是我现在陷入困境,因为我已经渲染了顶部菜单,但我找不到其他方法来做到这一点。

我不想使用大量 MySQL 查询,因为它会使我的网站变得非常慢。

我正在使用的代码:

require 'db.php';

$result = mysql_query("SELECT COUNT(c.category_id) AS count, c. category_id, c.parent_id, cd.name, p.product_id FROM category c
    LEFT JOIN category_description AS cd ON (cd.category_id=c.category_id)
    LEFT JOIN product_to_category AS ptc ON (ptc.category_id=c.category_id)
    LEFT JOIN product AS P ON (p.product_id=ptc.product_id) 
    GROUP BY c.category_id    
    ORDER BY c.parent_id,cd.name") or die (mysql_error()); 

$menuData = array( 'items' => array(), 'parents' => array() );

while ($menuItem = mysql_fetch_assoc($result)) {
    $menuData['items'][$menuItem['category_id']] = $menuItem;
    $menuData['parents'][$menuItem['parent_id']][] = $menuItem['category_id'];
}

function buildMenu($parentId, $menuData) {
    $html = '';
    if (isset($menuData['parents'][$parentId]))
    {
        $html = '<ul>';

        foreach ($menuData['parents'][$parentId] as $itemId) {
            $iCount  = ($menuData['items'][$itemId]['product_id'] != NULL) ? $menuData['items'][$itemId]['count'] : '0';

            $html   .= '<li>' . $menuData['items'][$itemId]['name'] . ' (' . $iCount . ') ';
            $html   .= buildMenu($itemId, $menuData);
            $html   .= '</li>';
        }

        $html .= '</ul>';
    }
    return $html;
}
echo buildMenu(0, $menuData);

预期输出:

Dell (1)
--Computer(1)
---DataCable(1)
----Extra Sub (0)

当前输出:

Dell (0)
--Computer(0)
---DataCable(1)
----Extra Sub (0)

I want to create a recursive menu with the product totals included, however I am stuck now because I already rendered the Topmenu however i cant find another way to do it.

I dont want to use alot of MySQL Queries because it can make my site very slow.

The code i am using:

require 'db.php';

$result = mysql_query("SELECT COUNT(c.category_id) AS count, c. category_id, c.parent_id, cd.name, p.product_id FROM category c
    LEFT JOIN category_description AS cd ON (cd.category_id=c.category_id)
    LEFT JOIN product_to_category AS ptc ON (ptc.category_id=c.category_id)
    LEFT JOIN product AS P ON (p.product_id=ptc.product_id) 
    GROUP BY c.category_id    
    ORDER BY c.parent_id,cd.name") or die (mysql_error()); 

$menuData = array( 'items' => array(), 'parents' => array() );

while ($menuItem = mysql_fetch_assoc($result)) {
    $menuData['items'][$menuItem['category_id']] = $menuItem;
    $menuData['parents'][$menuItem['parent_id']][] = $menuItem['category_id'];
}

function buildMenu($parentId, $menuData) {
    $html = '';
    if (isset($menuData['parents'][$parentId]))
    {
        $html = '<ul>';

        foreach ($menuData['parents'][$parentId] as $itemId) {
            $iCount  = ($menuData['items'][$itemId]['product_id'] != NULL) ? $menuData['items'][$itemId]['count'] : '0';

            $html   .= '<li>' . $menuData['items'][$itemId]['name'] . ' (' . $iCount . ') ';
            $html   .= buildMenu($itemId, $menuData);
            $html   .= '</li>';
        }

        $html .= '</ul>';
    }
    return $html;
}
echo buildMenu(0, $menuData);

The expected output:

Dell (1)
--Computer(1)
---DataCable(1)
----Extra Sub (0)

Current output:

Dell (0)
--Computer(0)
---DataCable(1)
----Extra Sub (0)

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

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

发布评论

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

评论(2

情绪少女 2024-11-26 04:23:47

我认为您的查询返回错误的结果。使用
print '

';print_r($menuData);print '

'; 在调用 buildMenu() 函数之前查看查询是否返回正确的数据。

I think your query returns wrong result. Use
print '<pre>';print_r($menuData);print '</pre>'; before calling buildMenu() function and see whether query returns right data or not.

几味少女 2024-11-26 04:23:47

我认为这应该让您更接近正确的方向:

foreach ($menuData['parents'][$parentId] as $itemId) {
    $menu = buildMenu($itemId, $menuData);
    $item = $menuData['items'][$itemId];
    $iCount  = ($item['product_id'] != NULL) ? $item['count'] : '0';
    $menuData['items'][$parentId]['count'] += $iCount;
    $html   .= '<li>' . $item['name'] . ' (' . $iCount . ') ';
    $html   .= $menu
    $html   .= '</li>';
}

通过重新排序然后将当前项目的计数添加到父项目的计数中,您可以确保在输出 iCount 时,它也将包括所有子项目的计数。

我还为 $menuData['items'][$itemId] 使用了临时变量。因为您只对该 $menuData 执行三个数组查找,所以它的效率可能不会更高,但更容易阅读。

I think this should get you closer to the right direction:

foreach ($menuData['parents'][$parentId] as $itemId) {
    $menu = buildMenu($itemId, $menuData);
    $item = $menuData['items'][$itemId];
    $iCount  = ($item['product_id'] != NULL) ? $item['count'] : '0';
    $menuData['items'][$parentId]['count'] += $iCount;
    $html   .= '<li>' . $item['name'] . ' (' . $iCount . ') ';
    $html   .= $menu
    $html   .= '</li>';
}

By re-ordering and then adding the current item's count to the parent item's count, you can make sure that when you output iCount, it will include all of the children's counts as well.

I also used a temporary variable for $menuData['items'][$itemId]. Because you're only doing three array lookups on that $menuData, it might not be substantially more efficient, but it is much easier to read.

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