2017-03-24 45 views
0

這是我當前使用的用於抓取我的php下拉列表的數據的SQL表。這樣做的主要目的是在基本上最終的結果把它變成具有與下面等成員的OPTGROUP下拉...PHP中的Dynamic Optgroup

+-----------+------------+-----------+ 
 
| GroupName | MemberName | ValueName | 
 
+-----------+------------+-----------+ 
 
| 1st Team | Joe Bob | Joe  | 
 
| 1st Team | Catherine | Kat  | 
 
| 2nd Team | Tommy  | Tom  | 
 
| 3rd Team | John Razks | John  | 
 
+-----------+------------+-----------+ 
 
Table name: Members

是這樣下面的代碼的。這將是一個名爲「1st Team」的optgroup的下拉菜單,並且具有下面的成員等。第二隊和第三隊等等。

<optgroup class="1st Team"> 
 
    <option value="Joe">Joe Bob</option> 
 
    <option value="Kat">Catherine</option> 
 
</optgroup> 
 
<optgroup class="2nd Team"> 
 
    <option value="Tom">Tommy</option> 
 
</optgroup> 
 
<optgroup class="3rd Team"> 
 
    <option value="John">John Razks</option> 
 
</optgroup>

現在,這是我從我的SQL表的信息。這工作正常,但如果我想添加一個新的GroupName,那麼我將不得不添加一個新的代碼到我的主頁,我不想那樣做。

嘗試使其成爲動態的,因此如果SQL表獲取新的GroupName更新,那麼新的optgroup類將出現在下拉列表中,並且成員將在下面。

<optgroup class="1st Team"> 
 
\t \t <?php 
 
\t \t \t $conn = mysqli_connect("#connect_to_sql"); 
 

 
\t \t \t if(!$conn){ 
 
\t \t \t \t die("Connection Failed".myslqi_connect_error()); 
 
\t \t \t } 
 
\t \t \t \t $result = mysqli_query($conn, "SELECT distinct MemberName from Members where GroupName = "1st Team" order by MemberName ASC"); 
 
\t \t \t \t while ($row = mysqli_fetch_assoc($result)){ 
 
\t \t \t \t unset($membername, $groupname); 
 
\t \t \t \t $groupname = $row['GroupName']; 
 
\t \t \t \t $membername = $row['MemberName']; 
 
\t \t \t \t echo '<option value="'.$membername.'">'.$membername.'</option>'; 
 
\t \t \t } 
 
\t \t ?> \t 
 
</optgroup>

我還不能肯定該怎麼做的。我看過其他人的例子,但不知道如何處理這一步驟。

回答

1

這不是最優雅的方式,但是你可以用這種方式構建你的組,它正在做的是檢查組的名稱並在每次名稱更改時將其更改爲新的optgroup,$ first var就在那裏所以它不會在第一次添加結束標籤。

我相信你可以改善這一點,但它應該讓你去。

它確實依賴於一致的命名約定,因爲我說,我相信你可以改進它。我還沒有包括連接檢查爲你做到這一點已經在你的榜樣

$result = mysqli_query($conn, "SELECT * FROM Members ORDER BY GroupName"); 
$groupName = ''; 
$first = true; 
echo '<select>'; 
while ($row = mysqli_fetch_assoc($result)){ 
    if ($row['GroupName'] != $groupName) { 
     $groupName = $row['GroupName']; // Just set the new group name 
     if (!$first) { // Add a closing tag when we change the group, but only if we're not in the first loop 
      echo '</optgroup>'; 
     } else { 
      $first = false; // Make sure we don't close the tag first time, but do after the first loop 
     } 
     echo '<optgroup label="' . $groupName . '">'; 
    } 
    // We want to echo the options every loop so it's outside the if condition 
    echo '<option value="' . $row['MemberName'] . '">' . $row['ValueName'] . '</option>'; 
} 
echo '</select>'; 
1

首先從數據庫中獲取的所有記錄。創建一個鍵爲組名的數組。循環新數組以生成所需的輸出。

// query to get all records from database table 
$result = mysqli_query($conn, "SELECT distinct MemberName,GroupName,ValueName from Members order by MemberName ASC"); 
while ($row = mysqli_fetch_assoc($result)){ 
    // generate an array with keys as group name 
    $array[$row['GroupName']][] = $row; 
} 

// loop the array to create optgroup 
foreach($array as $key=>$value){ 
    // check if its an array 
    if(is_array($value)){ 
     // create optgroup for each groupname 
     echo "<optgroup class='".$key."'>"; 
     foreach($value as $k=>$v){ 
      echo "<option value='".$v['membername']."'>'".$v['membername']."'</option>"; 
     } 
     echo "</optgroup>"; 
    } 
} 

我還沒有測試過,但肯定這會幫助你。你可以改進。