2012-10-11 51 views
3

我有一个下拉菜单,带有一个PHP'foreach',我循环来填充选择选项。这很好。但是,我想要做的是使用“optgroup”选项的各种子标签。在我返回的数组中,我有一个“蓝色”,“绿色”和“红色”的“类型”。我如何根据$ album ['type']将选项分成多个组?选择下拉PHP foreach循环与'optgroup'选项

这是我的代码:

$query = mysql_query("SELECT * FROM background_albums); 
$albums = array(); 
while($row = mysql_fetch_assoc($query)) { 
    array_push($albums, $row); 
} 

<select name="backgroundList" id="backgroundList"> 
    <? foreach($albums as $album) { ?> 
    <option value="<?=($row['id']);?>"><?=$row['title'];?></option> 
    <? } ?> 
</select> 

这是我想的foreach循环中实现的东西的那种:

if ($album['type'] == 'green')... 

<optgroup label="Green Backgrounds"> 
     <option>1</option> 
     <option>2</option> 
     <option>3</option> 
     <option>4</option> 
</optgroup> 

if ($album['type'] == 'blue')... 

<optgroup label="Blue Backgrounds"> 
     <option>5</option> 
     <option>6</option> 
     <option>7</option> 
     <option>8</option> 
</optgroup> 

回答

12

尝试

<select name="backgroundList" id="backgroundList"> 
<?php 
$album_type = ''; 
foreach ($albums as $album) { 
    if ($album_type != $album['type']) { 
    if ($album_type != '') { 
     echo '</optgroup>'; 
    } 
    echo '<optgroup label="'.ucfirst($album['type']).' Backgrounds">'; 
    } 
    echo '<option value="'.$album['id'].'">'.htmlspecialchars($album['title']).'</option>'; 
    $album_type = $album['type'];  
} 
if ($album_type != '') { 
    echo '</optgroup>'; 
} 
?> 
</select> 
+0

谢谢!这很好用! – Chris

4

检查是否从以前的变化类型值,然后发出相应的标签。

按类型查询:

$query = mysql_query("SELECT * FROM background_albums order by type"); //ordered query 

[...] 

$type = null; 

foreach ($albums as $album) { 
    if ($album['type'] != $type) { // type changed 
    if ($type !== null) { 
     echo '</optgroup>'; // close previous optgroup 
    } 
    $type = $album['type']; 
    echo '<optgroup label="' . htmlspecialchars($type) . '">'; // start optgroup 
    } 

    [...] 
} 

echo '</optgroup>'; // close last optgroup 
+0

对于这个peroperly工作,SELECT应该有 “按类型顺序”,是否正确? – janenz00

+0

谢谢,Tagrin! – Chris

2

上网查询各个岗位的加载后...然后试错了很多,我终于找到了OPTGROUP解决方案效果很好。

这段代码与tagrin0非常相似,但我想我会提供一些更多评论来帮助其他noobs,可能需要清晰地了解如何使这项工作没有太多的嘲讽和解释码。

希望它可以帮助别人。

这里的表结构...

opt_id | grp_id | optName | grpName | optAbbrv ...

echo '<select id="xxx" class="xxx"> select">'; 
echo '<option >Select an instance...</option>'; 

$dbc = mysqli_connect('localhost','xxx','xxx','xxx') or die('Error connecting to MySQL server.'); 

// run your query, something like 
// "SELECT <optgroup>_ID, <option>_ID, <optgroup>_Name, <ption>_Name FROM [mysql table this data is stored in] ORDER BY groupID, optionID" 

$select = "SELECT * FROM ci_opt ORDER BY grp_id, opt_id"; 

    $data = @mysqli_query($dbc, $select) or die(mysql_error()); 

    // <optgroup> of the previous <option> 
    $previous = ""; 

    // variable to set the first group 
    $first_group = true; 

    while($row = mysqli_fetch_array($data)) { 

     // if this <option> changed <optgroup> from the previous one, 
     // then add a new <optgroup> 
     if ($row['grpName'] != $previous) { 
      if (!$first_group) { 
       echo '</optgroup>'; 
      } else { 
       $first_group = false; 
      } 
      echo '<optgroup label="' . $row['grpName'] . '">'; 
      $previous = $row['grpName']; 
     } 

     // echo the current <option> 
     // Note: I've also prepared the [onclick] for linking the select to an event 

     echo '<option id="'. $row['optAbbrv'] .'" onclick="javascript:void()">' . ucwords($row['optName']) . '</option>'; 

    } 
    // close the last <optgroup> tag 
    echo '</optgroup>'; 
    // close the last <select> tag 
    echo '</select>'; 
    // close the connection 
    mysqli_close($dbc); 

//结束PHP