title: Issue In joomla custom dropdown list development
body: <p>For the past 3 days, I've been stuck on a dropdown list development using joomla 2.5, I have to retrieve data from database and show this data in a drop down the steps I followed are mentioned below:</p>
  
  <p>Inside the models folder I have created a new model inside fields folder and name this file "fieldname.php"</p>
  
  <p>Now the file "Models/fields/fieldname.php" contains following source code:</p>
  
  <pre><code>    &lt;?php
  
      defined('JPATH_BASE') or die;
  
      jimport('joomla.html.html');
      jimport('joomla.form.formfield');
      jimport('joomla.form.helper');
      JFormHelper::loadFieldClass('list');
  
      class JFormFieldMyCompany extends JFormFieldList
      {
  
              protected $type = 'MyCompany';
  
               public function getOptions()
              {
                      // Initialize variables.
                      $options = array();
  
                      $db     = JFactory::getDbo();
                      $query  = $db-&gt;getQuery(true);
  
                      $query-&gt;select('id As value,name As text');
                      $query-&gt;from('#_k2_tags AS a');
                      $query-&gt;order('a.name');
                      $db = $this-&gt;getDbo();
  
                      // Get the options.
                      $db-&gt;setQuery($query);
  
                      $options = $db-&gt;loadObjectList();
  
                      // Check for a database error.
                      if ($db-&gt;getErrorNum()) {
                              JError::raiseWarning(500, $db-&gt;getErrorMsg());
                      }
                      print_r($options);exit;
                      return $options;
              }
      }
  </code></pre>
  
  <p>after that inside my model filter.php I added the following code.</p>
  
  <p><strong>Models/filter.php</strong>:</p>
  
  <pre><code>&lt;?php
  defined( '_JEXEC' ) or die;
  
  jimport('joomla.application.component.modeladmin');
  
  class FiltersModelFilter extends JModelAdmin
  {
  
      //Add this handy array with database fields to search in
          protected $searchInFields = array('text','a.name');
  
  //Override construct to allow filtering and ordering on our fields
          public function __construct($config = array()) {
                  $config['filter_fields']=array_merge($this-&gt;searchInFields,array('a.name'));
                  parent::__construct($config);
          }
  
      public function getTable($type = 'Filter', $prefix = 'FiltersTable', $config = array())
      {
          return JTable::getInstance($type, $prefix, $config);
      }
  
  
  
  
      protected function loadFormData()
      {
          $data = JFactory::getApplication()-&gt;getUserState('com_filters.edit.filter.data', array());
  
          if (empty($data)) {
              $data = $this-&gt;getItem();
          }
  
          return $data;
      }
      public function getForm($data = array(), $loadData = true)
      {
          $form = $this-&gt;loadForm('com_filters.filter', 'filter', array('control' =&gt; 'jform', 'load_data' =&gt; $loadData));
  
          return $form;
      }
       protected function getListQuery(){
                  $db = JFactory::getDBO();
                  $query = $db-&gt;getQuery(true);
  
                  //CHANGE THIS QUERY AS YOU NEED...
                  $query-&gt;select('id As value, name As text')
                          -&gt;from('#_k2_tags AS a');
  
  
  
  
                  // Filter search // Extra: Search more than one fields and for multiple words
                  $regex = str_replace(' ', '|', $this-&gt;getState('filter.search'));
                  if (!empty($regex)) {
                          $regex=' REGEXP '.$db-&gt;quote($regex);
                          $query-&gt;where('('.implode($regex.' OR ',$this-&gt;searchInFields).$regex.')');
                  }
  
                  // Filter company
                  $company= $db-&gt;escape($this-&gt;getState('filter.name'));
                  if (!empty($company)) {
                          $query-&gt;where('(a.name='.$company.')');
                  }
  
                  // Filter by state (published, trashed, etc.)
                  $state = $db-&gt;escape($this-&gt;getState('filter.state'));
                  if (is_numeric($state)) {
                          $query-&gt;where('a.published = ' . (int) $state);
                  }
                  elseif ($state === '') {
                          $query-&gt;where('(a.published = 0 OR a.published = 1)');
                  }
  
                  //echo $db-&gt;replacePrefix( (string) $query );//debug
                  return $query;
          }
  
          /**
           * Method to auto-populate the model state.
           *
           * Note. Calling getState in this method will result in recursion.
           *
           * @since       1.6
           */
          protected function populateState($ordering = null, $direction = null)
          {
                  // Initialise variables.
                  $app = JFactory::getApplication('administrator');
  
                  // Load the filter state.
                  $search = $this-&gt;getUserStateFromRequest($this-&gt;context.'.filter.search', 'filter_search');
                  //Omit double (white-)spaces and set state
                  $this-&gt;setState('filter.search', preg_replace('/\s+/',' ', $search));
  
                  //Filter (dropdown) state
                  $state = $this-&gt;getUserStateFromRequest($this-&gt;context.'.filter.published', 'filter_state', '', 'string');
                  $this-&gt;setState('filter.state', $state);
  
                  //Filter (dropdown) company
                  $state = $this-&gt;getUserStateFromRequest($this-&gt;context.'.filter.name', 'filter_company', '', 'string');
                  $this-&gt;setState('filter.name', $state);
  
                  //Takes care of states: list. limit / start / ordering / direction
                  parent::populateState('a.name', 'asc');
          }
  
  }
  </code></pre>
  
  <p>Inside the "Views/filter/view.html.php"</p>
  
  <pre><code>&lt;?php
  defined( '_JEXEC' ) or die;
  
  jimport( 'joomla.application.component.view');
  
  class FiltersViewFilter extends JView
  {
      protected $item;
      protected $form;
      protected $state;
      protected $sortColumn;
      protected $sortDirection;
      protected $searchterms;
  
      public function display($tpl = null)
      {
  
          $this-&gt;item = $this-&gt;get('Item');
          $this-&gt;state = $this-&gt;get('State');
          $this-&gt;form = $this-&gt;get('Form');
           $this-&gt;state= $this-&gt;get('State');
  
                  //Following variables used more than once
                  $this-&gt;sortColumn = $this-&gt;state-&gt;get('list.ordering');
                  $this-&gt;sortDirection= $this-&gt;state-&gt;get('list.direction');
                  $this-&gt;searchterms= $this-&gt;state-&gt;get('filter.search');
          $this-&gt;addToolbar();
  
        parent::display($tpl);
      }
  
      public function addToolbar()
      {
          if ($this-&gt;item-&gt;ID) {
              JToolBarHelper::title(JText::_('Filter Title'));
          } else {
              JToolBarHelper::title(JText::_('Add Filter Title'));
          }
  
          JToolBarHelper::apply('filter.apply', 'JTOOLBAR_APPLY');
          JToolBarHelper::save('filter.save', 'JTOOLBAR_SAVE');
          JToolBarHelper::save2new('filter.save2new', 'JTOOLBAR_SAVE_AND_NEW');
  
          JToolBarHelper::cancel('filter.cancel');
      }
  }
  </code></pre>
  
  <p>inside the <strong>views/filter/tmpl/default.php</strong></p>
  
  <pre><code>&lt;?php defined( '_JEXEC' ) or die; 
  //Get companie options
  JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
  $companies = JFormHelper::loadFieldType('MyCompany', false);
  $companyOptions=$companies-&gt;getOptions(); // works only if you set your field getOptions on public!!
  //Get companie options
  ?&gt;
  &lt;form action="index.php?option=com_filters&amp;amp;ID=&lt;?php echo $this-&gt;item-&gt;ID ?&gt;"
      method="post" name="adminForm" class="form-validate"&gt;
  
     &lt;fieldset id="filter-bar"&gt;
                  &lt;div class="filter-search fltlft"&gt;
                          &lt;input type="text" name="filter_search" id="filter_search" value="&lt;?php echo $this-&gt;escape($this-&gt;searchterms); ?&gt;" title="&lt;?php echo JText::_('Search in Names, etc.'); ?&gt;" /&gt;
                          &lt;button type="submit"&gt;
                                  &lt;?php echo JText::_('JSEARCH_FILTER_SUBMIT'); ?&gt;
                          &lt;/button&gt;
                          &lt;button type="button" onclick="document.id('filter_search').value='';this.form.submit();"&gt;
                                  &lt;?php echo JText::_('JSEARCH_FILTER_CLEAR'); ?&gt;
                          &lt;/button&gt;
                  &lt;/div&gt;
                  &lt;div class="filter-select fltrt"&gt;
                          &lt;select name="filter_state" class="inputbox" onchange="this.form.submit()"&gt;
                                  &lt;option value=""&gt;
                                          &lt;?php echo JText::_('JOPTION_SELECT_PUBLISHED');?&gt;
                                  &lt;/option&gt;
                                  &lt;?php echo JHtml::_('select.options', JHtml::_('jgrid.publishedOptions', array('archived'=&gt;false)), 'value', 'text', $this-&gt;state-&gt;get('filter.published'), true);?&gt;
                          &lt;/select&gt;
  
                          &lt;select name="filter_type" class="inputbox" onchange="this.form.submit()"&gt;
                                  &lt;option value=""&gt; - Select Company - &lt;/option&gt;
                                  &lt;?php echo JHtml::_('select.options', $companyOptions, 'value', 'text', $this-&gt;state-&gt;get('filter.name'));?&gt;
                          &lt;/select&gt;
  
                  &lt;/div&gt;
          &lt;/fieldset&gt;
  &lt;div class="width-60 fltlft"&gt;
          &lt;fieldset class="adminform"&gt;
              &lt;ul class="adminformlist"&gt;
                  &lt;?php foreach ($this-&gt;form-&gt;getFieldset() as $field): ?&gt;
                      &lt;li&gt;&lt;?php echo $field-&gt;label; ?&gt;
                      &lt;?php echo $field-&gt;input; ?&gt;&lt;/li&gt;
                  &lt;?php endforeach ?&gt;
              &lt;/ul&gt;
  
          &lt;/fieldset&gt;
      &lt;/div&gt;
  
      &lt;input type="hidden" name="task" value="" /&gt;
      &lt;?php echo JHtml::_('form.token'); ?&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Please help me in identification of my error I need to sort out issue as soon as possible.</p>