数据表是这样的:
id | 分类 | 标题
1 a 11111
2 a 2222222222
3 a 3333333333
4 b 444444444
5 b 555555555
输出结果应该是
<select>
<optgroup label="a">
<option>11111</option>
<option>22222222</option>
<option>33333</option>
</optgroup><optgroup label="b">
<option>444444</option>
<option>55555555</option>
</optgroup>
</select>如何输出?
id | 分类 | 标题
1 a 11111
2 a 2222222222
3 a 3333333333
4 b 444444444
5 b 555555555
输出结果应该是
<select>
<optgroup label="a">
<option>11111</option>
<option>22222222</option>
<option>33333</option>
</optgroup><optgroup label="b">
<option>444444</option>
<option>55555555</option>
</optgroup>
</select>如何输出?
id | 分类 | 标题
1 a 11111
2 a 2222222222
3 a 3333333333
4 b 444444444
5 b 555555555
$last = '';
while*$r = mysql_fetch_assoc($rs)) {
if($r['分类'] != #last) {
if($last) echo '</optgroup>';
echo "<optgroup label='$r[分类]'>";
$last = $r['分类'];
}
echo "<option>$r[标题]</option>";
}
echo '</optgroup>';
echo '</select>';
*$r
是什么意思?
$last = '';
while($r = mysql_fetch_assoc($rs)) {
if($r['分类'] != $last) {
if($last) echo '</optgroup>';
echo "<optgroup label='$r[分类]'>";
$last = $r['分类'];
}
echo "<option>$r[标题]</option>";
}
echo '</optgroup>';
echo '</select>';