简体   繁体   English

Smarty While 嵌套循环仅显示第一行

[英]Smarty While Nested Loop Show Only First Row

I want to have a category and it's sub-category and i'm using smarty.我想要一个类别,它是子类别,我正在使用 smarty。 Here's the code that I use to show the data.这是我用来显示数据的代码。

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); $smarty->assign('groups', $res_groups);

From that code, I got result like this从那个代码,我得到了这样的结果

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

What I want is to have all groups that is in my database, and it should be like this我想要的是拥有我数据库中的所有组,它应该是这样的

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)

Someone can help me out from this problem?有人可以帮我解决这个问题吗?

After a day searching for an answer, I found my own answer.经过一天的寻找答案,我找到了自己的答案。 Here is the working code这是工作代码

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;
}

hope this help others希望这对其他人有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM