Life, SAP, Consulting, Programming, Coding, ASP.NET, Sharepoint, MVC, Javascript, PHP, WebDesign, CSS, HTML

I am new to CakePHP. It is not easy to code with it as I thought. Today,  I try to create a select box in PHP.  But When I add unicode text to options value, the box get empty options.

Here is explain with code:

This code give 2 blank options, and empty option as “Select City”:

$cities = array('1'=>'Hà Nội','2'=> 'Hồ Chí Minh');
echo $this->Form->input('city_id', array(
'label' => __d('users', 'City'),
'options' => $cities,
'empty' => __d('users', 'Select City'),

This is output of those code:


These codes give correct options:

**$cities = array('1'=>'Ha Noi','2'=> 'Ho Chi Minh');**
echo $this->Form->input('city_id', array(
'label' => __d('users', 'City'),
'options' => $cities,
'empty' => __d('users', 'Select City'),

Please note the different line of code: $cities = array(‘1’=>’Ha Noi’,’2’=> ‘Ho Chi Minh’);

The cause of  issue is that I didn’t tell Cake to support Unicode (not in front end HTML code). The solution, seemingly not related to the issue is to tell database encoding UTF8. To do so, you locate the file /app/Config/database.php.

Then add this line to database config variable: ‘encoding’ => ‘utf8’,

It will look like this:

public $default = array(
‘datasource’ => ‘Database/Mysql’,
‘persistent’ => false,
‘host’ => ‘localhost’,
‘login’ => ‘dbuser’,
‘password’ => ‘dbpass’,
‘database’ => ‘dbname’,
‘prefix’ => ”,
‘encoding’ => ‘utf8’,

Now you can see the correct output.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: