2012-11-21 15 views
-1

嗨,我有這個MySQL表。如何使用optgroup生成動態選擇框

CREATE TABLE IF NOT EXISTS `selling_counties` (
    `county_id` int(11) NOT NULL auto_increment, 
    `country` varchar(20) collate utf8_unicode_ci NOT NULL, 
    `county_name` varchar(25) collate utf8_unicode_ci NOT NULL, 
    `datecreated` datetime NOT NULL, 
    `datemodified` datetime NOT NULL, 
    PRIMARY KEY (`county_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=97 ; 

,我需要有OPTGROUP一個選擇框,其中國家將會和COUNTY_NAME將在S 如

<select name ="county"> 
    <optgroup label="England"> 
     <option>South Yorkshire</option> 
     <option>Staffordshire</option> 
     <option>Suffolk</option> 
     <option>Surrey</option> 
     <option>Tyne and Wear</option> 
     <option>Warwickshire</option> 
     <option>West Midlands</option> 
     <option>West Sussex</option> 
     <option>West Yorkshire</option> 
     <option>Wiltshire</option> 
     <option>Worcestershire</option> 
    </optgroup> 
    <optgroup label="Wales"> 
     <option>Clwyd</option> 
     <option>Dyfed</option> 
     <option>Gwent</option> 
     <option>Gwynedd</option> 
     <option>Mid Glamorgan</option> 
     <option>Powys</option> 
     <option>South Glamorgan</option> 
     <option>West Glamorgan</option> 
    </optgroup> 
    <optgroup label="Scotland"> 
     <option>Aberdeenshire</option> 
     <option>Angus</option> 
     <option>Argyll</option> 
     <option>Selkirkshire</option> 
     <option>Shetland</option> 
     <option>Stirlingshire</option> 
     <option>Sutherland</option> 
     <option>West Lothian</option> 
     <option>Wigtownshire</option> 
    </optgroup> 
    <optgroup label="Northern Ireland"> 
     <option>Antrim</option> 
     <option>Armagh</option> 
     <option>Down</option> 
     <option>Fermanagh</option> 
     <option>Londonderry</option> 
     <option>Tyrone</option> 
    </optgroup> 
</select> 

我已經嘗試過這種PHP: Dynamic Drop down with optgroup 但它有點複雜,我爲我我是PHP的新手,而且它來自2個獨立的桌子,我的桌子是從同一個桌子來的。

任何幫助將非常可觀。

+0

請出示你的代碼 – GBD

回答

0

您需要從表

$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']); 
} 

選擇的值,並把這些在你的HTML選擇。

更新<option>添加county_id

<option value="<?php echo htmlentities($county[0]); ?>"><?php echo htmlentities($county[1]); ?></option> 
+0

你可以將plz粘貼html代碼,以便我可以試用它。謝謝 – Asnexplore

+0

@ user1530513您可以使用MrCode的html部分。這應該很好。 –

+0

感謝哥們,它像一個魅力工作。我非常感謝你的幫助。 – Asnexplore

2

創建一個像這樣的新數組,以國家爲關鍵字,並將縣的數組作爲每個值。假設$rows是原始表格行的數組。

<?php 

$countries = array(); 

foreach($rows as $row) 
{ 
    if(isset($countries[$row['country']])) // <-- edit, was missing a ] 
    { 
     $contries[$row['country']][] = $row['county_name']; 
    } 
    else 
    { 
     $countries[$row['country']] = array($row['county_name']); 
    } 
} 

?> 

<select name ="county"> 
    <?php foreach($countries as $country => $counties): ?> 

     <optgroup label="<?php echo htmlentities($country); ?>"> 

      <?php foreach($counties as $county): ?> 

       <option><?php echo htmlentities($county); ?></option> 

      <?php endforeach; ?> 

     </optgroup> 

    <?php endforeach; ?> 
</select> 
+0

似乎有什麼不對勁解決此行$ contries [$行[ '國家'] [] = $行[ 'COUNTY_NAME']; – Asnexplore

+0

@ user1530513我在'isset()'行缺少一個']'。再試一次。 – MrCode

+0

感謝您的善良幫助哥們。我用它的一部分來使它工作。非常感謝。 – Asnexplore

相關問題