PDO 查询的分组结果问题

问题描述 投票:0回答:3

我的列表有问题,我想显示组和子组(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>';
php mysql pdo grouping html-select
3个回答
1
投票

假设

$groupe_tran
是你的
GROUP1
/
GROUP2
值,你可以保存之前的
$groupe_tran
并检查当前值是否不一样 -

$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();

// create a previous value
$previous_groupe_tran = '';

echo '<select name="groupe">';
foreach ($sRes as $sV) {

    $groupe_tran = $sV['groupe_tran'];
    $sgroupe_tran = $sV['sgroupe_tran'];

    // check if current matches previous, and only print if no match    
    if($groupe_tran != $previous_groupe_tran){

        echo '<option value="' . $groupe_tran . '">' . $groupe_tran . '</option>';

        // set previous to current
        $previous_groupe_tran = $groupe_tran;
    }

    echo '<option value="' . $sgroupe_tran . '">' . $sgroupe_tran . '</option>';
}
echo '</select>';

0
投票

我以你的例子并添加了 array_push:

    $previous_groupe_tran = array();

echo '<select name="hmenu">';
foreach ($sRes as $sV) {

$groupe_tran = $sV['groupe_tran'];
$sgroupe_tran = $sV['sgroupe_tran'];

if(!in_array($groupe_tran, $previous_groupe_tran)){
array_push($previous_groupe_tran, $groupe_tran);
print_r($previous_groupe_tran);
echo '<option value="' . $groupe_tran . '">'.$id_hm.' - ' . $groupe_tran . '</option>';
    }

    echo '<option value="' . $sgroupe_tran . '">'.$id_hm1.' - ' . $sgroupe_tran . '</option>';
}

0
投票

使用 PDO 的获取标志在关联的第一级数据下构建嵌套行。

代码:(PHPize演示

$sql = <<<SQL
SELECT 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
SQL;

echo "<select name=\"groupe\">\n";
    foreach ($pdo->query($sql)->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC) as $k => $rows) {
        echo "<option>$k</option>\n";
        foreach ($rows as $row) {
            echo "<option>{$row['sgroupe_tran']}</option>\n";
        }
    }
echo '</select>';

输出:

<select name="groupe">
<option>GROUP1</option>
<option>sgroup1-a</option>
<option>sgroup1-b</option>
<option>sgroup1-c</option>
<option>sgroup1-d</option>
<option>GROUP2</option>
<option>sgroup2-a</option>
<option>sgroup2-b</option>
</select>

您想使用

optgroup
标签吗?

© www.soinside.com 2019 - 2024. All rights reserved.