根据周,日选择optgroup匹配

时间:2016-04-02 18:39:43

标签: php mysql fetch

抱歉标题不好,我的英文不好。

<select class="chosen2" name="mac" onchange="this.form.submit()">
<option></option>
<?php 
error_reporting(0); 
$maclist = mysql_query("SELECT year,split,leagueid,takim,versus,mac,week,day
FROM league WHERE id >= 0 $ligsarry $yearsarrf $splitsarrf GROUP by mac ORDER BY hafta desc,gun desc,mac desc,side asc, id asc");
while ($macs=mysql_fetch_array($maclist)) {
?>
  <option <?php if (isset($_GET['mac']) && $_GET['mac']==$macs['mac']) {echo "selected='selected'"; } ?> value="<?php echo $macs['mac']; ?>"><?php echo $macs['mac'] . " -- W" . $macs['week'] . "D" . $macs['day'];;?></option>

<?php }?>
</select>

我用这段代码得到了这个结果:

enter image description here

我想添加<optgroup label="Week 1 Day 2"> -w1d2 matches- </optgroup>
<optgroup label="Week 1 Day 1"> -w1d1 matches- </optgroup>

但我不知道该怎么做。一天中的匹配计数并不总是4。

1 个答案:

答案 0 :(得分:1)

我认为这应该有效:

$current_day = 0;

while (...) {

    if ($macs['day'] != $current_day) {

        if ($current_day > 0) {
            print '</optgroup>';
        }            

        print '<optgroup>';

        $current_day = $macs['day'];
    }

    print '<option>...</option>';

}

if ($current_day > 0) {
    print '</optgroup>';
}

每次阅读新$macs['day']时,都会打印<optgroup>