Smarty 嵌套循环仅显示第一行

Smarty While Nested Loop Show Only First Row

我想要一个类别和它的子类别,我正在使用 smarty。这是我用来显示数据的代码。

global $conn;
$res_groups = array();
$stmt = $conn->prepare("
    SELECT * FROM groups
");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while ($group = $stmt->fetch()){
    $groups = array();
    $groupsID = $group['id'];
    $groups['name'] = $group['name'];

    $programs = array();
    $stmt = $conn->prepare("
        SELECT * FROM listings WHERE group_id = '$groupsID'
    ");
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    while ($program = $stmt->fetch()){
        $programs[] = $program;
    }
    $groups['listings'] = $programs;
    $res_groups[] = $groups;
}

$smarty->assign('groups', $res_groups);

根据该代码,我得到了这样的结果

1. New Group
-- Sub category
-- Sub category

我想要的是拥有我数据库中的所有组,它应该是这样的

1. New Group
-- Sub category
-- Sub category

2. Second Group
-- Sub category
-- Sub category

3. Third Group
-- Sub category
 (and so on for Group 4,5,6)

有人可以帮我解决这个问题吗?

经过一天的寻找答案,我找到了自己的答案。这是工作代码

global $conn;
$res_groups = array();
$stmt = $conn->prepare("
    SELECT * FROM groups
");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while ($group = $stmt->fetch()){
    $groups = array();
    $groups['id'] = $group['id'];
    $groups['name'] = $group['name'];
    $groups['type'] = $group['type'];
    $groups['description'] = $group['description'];
    $groups['sort'] = $group['sort'];
    $groups['status'] = $group['status'];
    $groups['listing_reg_type'] = $group['listing_reg_type'];
    $groups['listing_per_page'] = $group['listing_per_page'];
    $groups['listing_fee'] = $group['listing_fee'];
    $groups['listing_reinvest__fee'] = $group['listing_reinvest_fee'];

    $programs = array();
    $stmts = $conn->prepare('
        SELECT * FROM listings WHERE group_id = "'.$groups['id'].'"
    ');
    $stmts->execute();
    $stmts->setFetchMode(PDO::FETCH_ASSOC);
    while ($program = $stmts->fetch()){
        $programs[] = $program;
    }
    $groups['listings'] = $programs;
    $res_groups[] = $groups;
}

希望这对其他人有帮助