1
Bluenoser
XoopsFormSelect formatting data
  • 2013/4/27 3:20

  • Bluenoser

  • Just popping in

  • Posts: 24

  • Since: 2007/6/5 1


I am trying to add additional information to the XoopsFormSelect. I am trying to add information to the value to colour each drop down element. Here is my code.

class GAFormSelecttee_names extends XoopsFormSelect {
      function 
GAFormSelecttee_names($caption, $name, $value=null, size=1, $nullopt=false, $otheropt=false) {
         
$db =& Database::getInstance();
         
$this->XoopsFormSelect($caption, $name, $value, $size);
         if (
$nullopt)
             
$this->addOption('','-');
         
$sql = "SELECT tee_name_id, name, fgcolour, bgcolour FROM ".$db->prefix("ga_tee_names")." ORDER BY name";
         
$result = $db->query($sql);
         while (
$myrow = $db->fetchArray($result)) {
           
$this->addOption($myrow['tee_name_id'].'" style="background-color:'.$myrow['bgcolour'].';color:'.$myrow['fgcolour'].';',$myrow['name']);
         }
      }
 }

The problem is that my " get converted to the HTML equiv of "

Anyway I can prevent this?

2
irmtfan
Re: XoopsFormSelect formatting data
  • 2013/4/27 11:04

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


you should just change the render function and add some functionality . just one line is enough
eg: like this:
xoops_load('XoopsFormSelect');
class 
MyModuleFormSelect extends XoopsFormSelect
{
   function 
render()
    {
        
$ele_name = $this->getName();
        
$ele_title = $this->getTitle();
        
$ele_value = $this->getValue();
        
$ele_options = $this->getOptions();
        
$ret = '<select size="' . $this->getSize() . '"' . $this->getExtra();
        if (
$this->isMultiple() != false) {
            
$ret .= ' name="' . $ele_name . '[]" id="' . $ele_name . '" title="'. $ele_title. '" multiple="multiple">' ;
        } else {
            
$ret .= ' name="' . $ele_name . '" id="' . $ele_name . '" title="'. $ele_title. '">' ;
        }
        foreach(
$ele_options as $value => $name) {
            
$ret .= '<option value="' . htmlspecialchars($value, ENT_QUOTES) . '"';
            
$ret .= ' id="' . $value . '"'; // add just this irmtfan
            
if (count($ele_value) > 0 && in_array($value, $ele_value)) {
                
$ret .= ' selected="selected"';
            }
            
$ret .= '>' . $name . '</option>' ;
        }
        
$ret .= '</select>';
        return 
$ret;
    }
}
Also I think you should change your mind a little.
I think it would be better to use only css and not store colors in database.
anyway the above could be used like this:
#ele_name option#value {
background-color:RED;
}
you can use css or database. what you like.