Here is code snippet:
<?php
// Set some default value to be the selected item in the dropdown list
$default = 3;
// An array of $key=>$value pairs
$cities = array(1 => 'Rawalpindi', 2 => 'Islamabad', 3 => 'Lahore', 4 => 'Karachi');
// Initialize array to store dropdown options
$options = array();
foreach($cities as $key=>$value) {
// Create $value
// For Joomla 3
$options[] = JHTML::_('select.option', $key, $value);
// For Joomla 4, Joomla 5+
// Please also add use Joomla\CMS\HTML\HTMLHelper;
// At the top of file but after namespace if it is present
$options[] = HTMLHelper::_('select.option', $key, $value);
}
// Create <select name="city" class="inputbox"></select>
// For Joomla 3
$dropdown = JHTML::_('select.genericlist', $options, 'city', 'class="inputbox"', 'value', 'text', $default);
// For Joomla 4, Joomla 5+
$attribs = null;
echo HTMLHelper::_('select.genericlist', $options, 'city', $attribs, 'value', 'text', $default);
// Output created <select> list
echo $dropdown;
?>
Hope this helped.
Still need help! no problem, feel free to contact us Today