2
我有一個列表中的一個問題,我想顯示組和子組(sgroups),是這樣的:問題與分組項
GROUP1
sgroup1-a
sgroup1-b
sgroup1-c
sgroup1-d
GROUP2
sgroup2-a
sgroup2-b
但我的代碼返回:
GROUP1
sgroup1-a
GROUP1
sgroup1-b
GROUP1
sgroup1-c
GROUP2
sgroup2-a
GROUP2
sgroup2-b
sgroup1-d
所以我有顯示組的名稱。
表結構:
CREATE TABLE IF NOT EXISTS `groupe` (
`id_hm` int(2) NOT NULL AUTO_INCREMENT,
`groupe_tran` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`onoff` enum('0','1') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1' COMMENT '0:no, 1:active',
`search` enum('0','1') COLLATE utf8_unicode_ci NOT NULL DEFAULT '0' COMMENT 'include in search menu, 0: 0ff, 1: on',
PRIMARY KEY (`id_hm`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=11 ;
CREATE TABLE IF NOT EXISTS `sgroupe` (
`id_bm` int(2) NOT NULL AUTO_INCREMENT,
`id_hm1` tinyint(2) NOT NULL COMMENT 'head menu id',
`sgroupe_tran` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`sgroupe_en` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`onoff` enum('0','1') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1' COMMENT '0:no, 1:active',
`search` enum('0','1') COLLATE utf8_unicode_ci NOT NULL DEFAULT '0' COMMENT 'include in search menu, 0: 0ff, 1: on',
PRIMARY KEY (`id_bm`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=49 ;
查詢和PHP代碼:
$sSel = 'SELECT DISTINCT(groupe_tran), sgroupe_tran FROM groupe JOIN sgroupe ON groupe.id_hm = sgroupe.id_hm1 WHERE groupe.search = "1" AND sgroupe.search = "1" AND groupe.onoff = "1" AND sgroupe.onoff = "1"';
$sReq = $connexion->query($sSel);
$sRes = $sReq->fetchAll();
echo '<select name="groupe">';
foreach ($sRes as $sV) {
$groupe_tran = $sV['groupe_tran'];
$sgroupe_tran = $sV['sgroupe_tran'];
echo '<option value="' . $groupe_tran . '">' . $groupe_tran . '</option>';
echo '<option value="' . $sgroupe_tran . '">' . $sgroupe_tran . '</option>';
}
echo '</select>';
您可以創建一個'$ previous_groupe_tran'並檢查當前是同前,如果沒有的話打印。 – Sean
可否請你舉個例子,我找不到它是如何工作的 –
發佈了一個例子作爲答案。 – Sean