简体   繁体   English

PHP中的动态Optgroup

[英]Dynamic Optgroup in PHP

This is my current SQL Table I'm using to grab data for my php dropdown. 这是我当前使用的SQL表,用于获取PHP下拉列表的数据。 The main goal of this is to turn this into a dropdown that has an optgroup with the members below and so on... 这样做的主要目的是将其变成一个下拉菜单,该下拉菜单具有一个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 

Basically at the end result is such of the code below. 基本上,最终的结果是下面的代码。 It will be a dropdown with an optgroup called "1st Team" and have the members below ect. 这将是一个名为“ 1st Team”的optgroup的下拉列表,其成员如下。 for 2nd Team and 3rd Team and so on. 代表二队和三队等等。

 <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> 

Right now, this is how I get information from my SQL table. 现在,这就是我从SQL表中获取信息的方式。 This works fine, but if I want to add a new GroupName then I would have to add a new code to my main page and I don't want to do that. 这可以正常工作,但是如果我想添加一个新的GroupName,那么我就必须在我的主页上添加一个新代码,而我不想这样做。

Trying make it dynamic so if the SQL table gets updated with a new GroupName, then a new optgroup class will appear in the dropdown and the members will be below. 尝试使其动态化,以便如果SQL表使用新的GroupName更新,则下拉列表中将出现一个新的optgroup类,而其成员将位于下面。

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

I'm not positive at all on what to do. 我根本不怎么做。 I've looked at other people's examples, but not sure how to approach this step. 我看过其他人的示例,但不确定如何执行此步骤。

It's not the most elegant way, but you could build your group this way, what it's doing is checking the name of the group and changes that to a new optgroup each time the name changes, the $first var is just there so it doesn't add a closing tag the first time around. 这不是最优雅的方法,但是您可以用这种方式建立您的组,它的工作是检查组的名称,并在每次名称更改时将其更改为新的optgroup,$ first var就在那里,所以它没有。 t第一次添加结束标记。

I'm sure you can improve on this, but it should get you going. 我敢肯定,您可以对此进行改进,但是它应该可以帮助您前进。

It does kind of rely on a consistent naming convention for the group name, so as I say, I'm sure you could improve on it. 它确实依赖于组名称的一致命名约定,因此,正如我所说,我相信您可以对此进行改进。 I've also not included the connection checks as you've done that already in your example 我还没有包括连接检查,因为您已经在示例中完成了连接检查

$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>';

First get all records from the database. 首先从数据库中获取所有记录。 create an array with keys as group name. 创建一个以键作为组名的数组。 Loop the new array to generate the desired output. 循环新数组以生成所需的输出。

// 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>";
    }
}

I have not tested this but sure this will help you. 我尚未对此进行测试,但是可以肯定会有所帮助。 And you can make improvements. 您可以进行改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM