2012-10-11 48 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