【问题标题】:for each category row products对于每个类别行产品
【发布时间】:2013-06-23 19:30:39
【问题描述】:

我有 3 张桌子。

module_names (module_id, module_name, module_content)

module_groups (group_id,group_name,group_content)

module_to_group (module_id,group_id)

我正在使用以下查询来获取结果:

$result = $db->sql_query("select 
m.module_id, 
m.module_name, 
g.group_name 
from 
" . $prefix . "_mmmodule_names m ,  
" . $prefix . "_mmmodule_to_group m2g, 
" . $prefix . "_mmmodule_groups g 
where 
m.module_id = m2g.module_id 
and 
m2g.group_id = g.group_id 
and 
g.group_id = g.group_id 
order by 
m.module_id"); 
while($row = $db->sql_fetchrow($result)) { 
$content .= '<div>' . $row['group_name'] . '</div>'; 

$content .= '<div>' . $row['module_name'] . '</div>'; 
} 
echo $content;

输出如下:

组 1

模块1

组 1

模块2

组2

模块3

组2

模块4

但我需要这样:

组 1

模块1

模块2

组2

模块3

模块4

很久以前我做了类似的东西。 对于我所记得的,我每个人都使用过。当然,我当时拥有的资源现在不再是谷歌的前 10 名搜索结果。 (Groupconcat 对我来说不是一个好主意)

【问题讨论】:

    标签: php mysql arrays foreach


    【解决方案1】:
    $result = $db->sql_query("select m.module_id, m.module_name, g.group_name from " . $prefix . "_mmmodule_names m ,  " . $prefix . "_mmmodule_to_group m2g, " . $prefix . "_mmmodule_groups g where m.module_id = m2g.module_id and m2g.group_id = g.group_id and g.group_id = g.group_id order by m.module_id"); 
    
    $modules = array();
    while ($row = $db->sql_fetchrow($result)) { 
        $modules[$row['group_name']][] = $row['module_name'];      
    } 
    
    foreach ($modules as $groupName => $moduleNames) {
    
        echo "<h2>$groupName</h2>";
    
        foreach ($moduleNames as $moduleName) {
            echo "<p>$moduleName</p>";
        }
    }
    

    仅仅记住最后一个组名是不够的,因为它们可以混合,另一种解决方案可能是在查询中为组名添加排序,在这种情况下,您可以确保组不会混合。

    【讨论】:

    • 接受了你的回答,因为我再次清楚了。这就是我记得的方式。答案给了我从中创建动态菜单的解决方案。谢谢!!!
    【解决方案2】:

    一切都很简单。只需“记住”最后一个组名,在获得下一个之前不要重复它。

    $tmpGroupName = null;
    $result = $db->sql_query("select m.module_id, m.module_name, g.group_name from " . $prefix . "_mmmodule_names m ,  " . $prefix . "_mmmodule_to_group m2g, " . $prefix . "_mmmodule_groups g where m.module_id = m2g.module_id and m2g.group_id = g.group_id and g.group_id = g.group_id order by m.module_id");
    while($row = $db->sql_fetchrow($result)) {
    
        if ($tmpGroupName != $row['group_name'])
        {
            $tmpGroupName = $row['group_name'];
            $content .= '<div>' . $row['group_name'] . '</div>';
        }
    
        $content .= '<div>' . $row['module_name'] . '</div>';
    }
    echo $content;
    

    【讨论】:

    • 当您的回答按照我的要求进行时,我无法从中创建动态菜单 li ul li li ul li.Ofcourse 也可能是我的知识缺失。所有这要怪我。
    【解决方案3】:

    解决方案可能是保存状态,我的意思是在while循环上方创建一个变量并保存组标题名称是它(例如Group 1),并且在第一次迭代时变量为空,您也可以打印标题但是对于下一次迭代检查并查看循环中的标题是否等于存储在变量中的内容是保存的状态。 如果它们相等,您可以再次传递打印标题,如果不是,则意味着它的新组即将到来,您也可以打印标题。

    关于内容(对于每个标题),您可以在每次迭代中无条件打印。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 2012-09-02
      • 1970-01-01
      相关资源
      最近更新 更多