How to generate dynamic selectbox with optgroup

前端 未结 2 587
野趣味
野趣味 2021-01-29 08:22

Hi I am having this mysql table.

CREATE TABLE IF NOT EXISTS `selling_counties` (
  `county_id` int(11) NOT NULL auto_increment,
  `country` varchar(20) collate u         


        
2条回答
  •  再見小時候
    2021-01-29 09:17

    You need to select the values from your table

    $mysqli = new mysqli($host, $user, $passwd, $database);
    $result = $mysqli->query('select county_id, country, county_name from selling_counties');
    $countries = array();
    while ($row = $result->fetch_assoc()) {
        $country = $row['country'];
        if (!array_key_exists($country, $countries))
            $countries[$country] = array();
    
        $countries[$country][] = array($row['county_id'], $row['county']);
    }
    

    and put these in your html select.

    Update to add county_id in :

    
    

提交回复
热议问题