Thursday, June 16, 2011

How to Create Joomla Select Box

How to create select box in joomla.
Simple Select box

$names = array(0 => "Select Name", 1 => 'Amit', 2 => 'Amit', 3 => 'Ajeet', 4 => 'Amar');
$options = array();
foreach($names as $key=>$value)
{
$options[] = JHTML::_('select.option', $key, $value);
}
$simple_dropdown = JHTML::_('select.genericlist', $options, 'name', 'class="inputbox"', 'value', 'text');
echo $simple_dropdown;

In joomla development, many time you need to show dynamic select box with the values from database.
Joomla drop down with values from database

$db = JFactory :: getDBO();
$query = "select * from #__users";
$db->setQuery($query);
$result = $db->loadObjectList();
$options = array();
$options[] = JHTML::_('select.option', '0', 'Select Name');
foreach($result as $row)
{
$options[] = JHTML::_('select.option', $row->id, $row->name);
}
$dropdown = JHTML::_('select.genericlist', $options, 'class="inputbox"', 'name', 'value', 'text');
echo $dropdown;

To select default option
$default = 62; // make sure this value must match
$dropdown = JHTML::_('select.genericlist', $options, 'class="inputbox"', 'name', 'value', 'text', $default);
echo $dropdown;

To use radio button instead of dropdown
$radio = JHTML::_('select.radiolist', $options, 'class="inputbox"', 'name', 'value', 'text', $default);
echo $radio;

To use radio button with newline.
add this line
$radio =str_replace('</label>','</label><br />', $radio); before echo $radio;
You can read more here.

No comments:

Post a Comment