I want to have a category and it's sub-category and i'm using 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);
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