0

谁能告诉我为什么这不起作用?错误出现在 foreach 循环中的某个地方。

jimport('joomla.form.formfield');

class JFormFieldCity extends JFormField {

        protected $type = 'city';

        public function getInput() {
        $db = JFactory::getDBO();
        $db->setQuery(
            'SELECT title' .
            ' FROM #__content'
        );
        $title = $db->loadObjectList;

                return '<select id="'.$this->id.'" name="'.$this->name.'">'.
                                           foreach ($title as $titlex)
                       {
                       '<option value="6" >'.$titlex.'</option>'
                       }
                       '</select>';} }
4

1 回答 1

1

下面是一个编辑过的代码,如果您从 xml 创建自定义字段,这将很有帮助。试一试,如果它不起作用,请告诉我。

     // No direct access to this file
        defined('_JEXEC') or die;

        // import the list field type


   jimport('joomla.form.helper');
    JFormHelper::loadFieldClass('list');


    class JFormFieldCity extends JFormFieldList
    {
        /**
         * The field type.

     *
     * @var     string
     */
    protected $type = 'city';

    /**
     * Method to get a list of options for a list input.
     *
     * @return  array       An array of JHtml options.
     */
    protected function getOptions() 
    {
        $db = JFactory::getDBO();
        //$query = new JDatabaseQuery;
        $query = $db->getQuery(true);
        $query->select('id,title');
        $query->from('#__content');         
        $query->order('title'); 
        $db->setQuery((string)$query);
        $messages = $db->loadObjectList();

        $options = array();
        if ($messages)
        {
            foreach($messages as $message) 
            {
                $options[] = JHtml::_('select.option', $message->title, $message->title);
            }
        }

        $options = array_merge(parent::getOptions(), $options);     
        return $options;
    }
}
于 2012-10-29T20:10:55.740 回答