[SOLVED] Passing pound symbol (£) through CActiveForm dropDownList

Hi there,

I have a dropdown list of money values, e.g.


up to £50,000

up to £100,000

up to £250,000

etc...

I’m creating the dropDownList using CActiveForm, but however I try to insert the pound symbol it doesn’t work.

I’ve tried;


$priceOptions=array("" => "All", "50000" => "Up to £50,000", "100000" => "Up to £100,000", "250000" => "Up to £250,000");

echo $form->dropDownList($model,'max_price',$priceOptions,array('options' => array(''=>array('selected'=>true))));

But this just displays all the lines as blank options with no text.

I’ve also tried;


$priceOptions=array("" => "All", "50000" => "Up to £50,000", "100000" => "Up to £100,000", "250000" => "Up to £250,000");

echo $form->dropDownList($model,'max_price',$priceOptions,array('options' => array(''=>array('selected'=>true))));

But this just displays the lines in plain text like: ‘Up to £50,000’.

I’ve also tried using the ‘& # 163 ;’ option for the pound symbol, but this just displays the lines in plain text again like: ‘Up to & # 163 ;50,000’ (without the spaces obviously).

Does anyone know how I can do this?

Thanks,

Stu

£ copy it

Hi Liu1084,

Thanks for the hint, but that comes out as a ‘?’ in notepad++. I copied it into notepad and it comes out as the small £ symbol we see here, but when uploaded it comes out as a ‘?’ on the rendered page too.

I’m not sure why this is happening, I guess the dropdownList function parses the contents of the options array somehow to remove html code, but not sure why it would remove a pound symbol.

It displays the dollar sign ($) fine, but both the pound (£) and euro (€) sign it doesn’t display, and it also removes all the text from the option that contains the pound or euro sign. Very strange.

Any more suggestions are more than welcome!

Thanks,

Stu

It’s an encoding issue using this function: http://code.google.com/p/yii/source/browse/tags/1.1.7/framework/web/helpers/CHtml.php#64

The Yii charset it uses is UTF-8 on my build, and I’ve heard this can cause issues with the pound symbol.

I’ve updated the CHtml.php helper file (/framework/web/helpers/CHtml.php) editing line 66 to:


return htmlspecialchars($text,ENT_QUOTES,'ISO-8859-15');

And also updated the encoding on my template file to the same ISO code, rather than UTF-8. Now when I insert the pound symbol directly into the code, it displays fine.

I think a better option may be to reset the Yii::app()->charset to the same ISO code as it’s used in a lot of encoding processes in /framework/web/helpers/CHtml.php, but I’ll deal with that if I come across any more errors!

public static function activeDropDownList($model, $attribute, $data, $htmlOptions = array()) {

    self::resolveNameID($model, $attribute, $htmlOptions);


    


    $selection = self::resolveValue($model, $attribute);


    $options = "\n" . self::listOptions($selection, $data, $htmlOptions);


    self::clientChange('change', $htmlOptions);


    if ($model->hasErrors($attribute))


        self::addErrorCss($htmlOptions);


    $hidden = '';


    if (!empty($htmlOptions['multiple'])) {


        if (substr($htmlOptions['name'], -2) !== '[]')


            $htmlOptions['name'].='[]';


        if (isset($htmlOptions['unselectValue'])) {


            $hiddenOptions = isset($htmlOptions['id']) ? array('id' => self::ID_PREFIX . $htmlOptions['id']) : array('id' => false);


            $hidden = self::hiddenField(substr($htmlOptions['name'], 0, -2), $htmlOptions['unselectValue'], $hiddenOptions);


            unset($htmlOptions['unselectValue']);


        }


    }


    return $hidden . self::tag('select', $htmlOptions, $options);


}


/**


 * 


 * @param type $selection


 * @param type $listData


 * @param type $htmlOptions


 * @return string


 */


public static function listOptions($selection, $listData, &$htmlOptions) {


    $raw=isset($htmlOptions['encode']) && !$htmlOptions['encode'];


    


    $content = '';


    if (isset($htmlOptions['prompt'])) {


        $content.='<option value="">' . strtr($htmlOptions['prompt'], array('<' => '<', '>' => '>')) . "</option>\n";


        unset($htmlOptions['prompt']);


    }


    if (isset($htmlOptions['empty'])) {


        if (!is_array($htmlOptions['empty']))


            $htmlOptions['empty'] = array('' => $htmlOptions['empty']);


        foreach ($htmlOptions['empty'] as $value => $label)


            $content.='<option value="' . self::encode($value) . '">' . strtr($label, array('<' => '<', '>' => '>')) . "</option>\n";


        unset($htmlOptions['empty']);


    }


    if (isset($htmlOptions['options'])) {


        $options = $htmlOptions['options'];


        unset($htmlOptions['options']);


    }


    else


        $options = array();


    $key = isset($htmlOptions['key']) ? $htmlOptions['key'] : 'primaryKey';


    if (is_array($selection)) {


        foreach ($selection as $i => $item) {


            if (is_object($item))


                $selection[$i] = $item->$key;


        }


    }


    elseif (is_object($selection))


        $selection = $selection->$key;


    foreach ($listData as $key => $value) {


        if (is_array($value)) {


            $content.='<optgroup label="' . ($raw ? $key : self::encode($key)) . "\">\n";


            $dummy = array('options' => $options);


            if (isset($htmlOptions['encode']))


                $dummy['encode'] = $htmlOptions['encode'];


            $content.=self::listOptions($selection, $value, $dummy);


            $content.='</optgroup>' . "\n";


        }


        else {


            $attributes = array('value' => (string) $key, 'encode' => !$raw);


            if (!is_array($selection) && !strcmp($key, $selection) || is_array($selection) && in_array($key, $selection))


                $attributes['selected'] = 'selected';


            if (isset($options[$key]))


                $attributes = array_merge($attributes, $options[$key]);


            $content.=self::tag('option', $attributes, $raw ? (string) $value : self::encode((string) $value)) . "\n";


        }


    }


    unset($htmlOptions['key']);


    return $content;


}

The line in listoption function having a problem

    $raw=isset($htmlOptions['encode']) && !$htmlOptions['encode'];

My query was returning good currency symbol in CHtmllistdata

I made this line to

    $raw=isset($htmlOptions['encode']);

Now its working

and i provide from my form is

<?php echo zHtml::activeDropDownList($model, ‘currency_id’, (BspCurrency::model()->getCurrencies()), array(‘class’ => ‘select2’, ‘encode’ => true)); ?>

Every thing is working now

I think its a yii bug