简体   繁体   中英

Dynamic Optgroup in PHP

This is my current SQL Table I'm using to grab data for my php dropdown. The main goal of this is to turn this into a dropdown that has an optgroup with the members below and so on...

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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