Search code examples
encodingdatatablesspecial-charactersutf8-decode

Encoding problems Datatables


I'm using datatables to show my records on my site, but when i'm entering in my input field those characters: ( € OR ‘ OR ’ ) my datatable isn't showing those characters. All other special characters are shown.

The rest of the website is showing all characters including: ( € OR ‘ OR ’ )?

static function data_output ( $columns, $data )
{
    $out = array();
    for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
        $row = array();

        for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {

            $column = $columns[$j];

            // Is there a formatter?
            if ( isset( $column['formatter'] ) ) {
                $row[ $column['dt'] ] = $column['formatter']( $data[$i][ $column['db'] ], $data[$i] );
            }
            else {
                $row[ $column['dt'] ] = $data[$i][ $columns[$j]['db'] ];
            }
        }

        $row = self::encodeutf8($row);

        $out[] = $row;
    }
    return $out;
}

private static function encodeutf8($d) {
    if (is_array($d)) {
        foreach ($d as $k => $v) {
            $d[$k] = self::encodeutf8($v);
        }
    } else if (is_string ($d)) {
        return utf8_encode($d);
    }
    return $d;
}

I added by myself the function : encodeutf8. I did this because before that, my table crashed everytime when i added an item with special characters in it.

My columns looks like this:

$columns = [
        ['db' => 'foto1', 'dt' => 0, 'formatter' => function($d, $row) {
            if($d == "") {
                return "";
            }
            return "<img class='thumb' src='/" . $d . "'/>";
        }],
        ['db' => 'p_categorie', 'dt' => 1],
        ['db' => 'p_genre', 'dt' => 2],
        ['db' => 'p_aantal', 'dt' => 3],
        ['db' => 'p_prijs', 'dt' => 4, 'formatter' => function($d, $row){
            if($d == '0.00'){
                return "N.o.t.k";
            }
            else{
                return "&euro;" .$d.",-";
            }
        }],
        ['db' => 'w_naam', 'dt' => 5],
        ['db' => 'p_artiest', 'dt' => 6, 'formatter' => function($d, $row){
            if($d != ''){
                return $d;
            }
            else{
                return "N.V.T.";
            }
        }],
        ['db' => 'p_medium', 'dt' => 7],
        ['db' => 'p_titel', 'dt' => 8],
        ['db' => 'p_conditie', 'dt' => 9],
        ['db' => 'p_bijzonderheid', 'dt' => 10],
        ['db' => 'p_datum', 'dt' => 11, 'formatter' => function($d, $row) {
            return "<a style='text-decoration:none; color:black;' title='".$d."'>".date('d-M-Y', strtotime($d))."</a>";
        }],
        ['db' => 'p_id', 'dt' => 12, 'formatter' => function($d, $row) {
            return "<a data-id='" . $d . "' href='/productdetail?p_id=" . $d . "'>Bekijk</a>";
        }],
    ];

I am looking forward to any reply!


Solution

  • I guess you are using a diversion of ssp.class.php using your own database-connection. Your problem is that you not have defined a proper charset for your connection - it should be set to utf8.

    I cannot know what underlying connection you are using, but you must setup the charset immediately after you have established the connection.

    • mysql
        mysql_set_charset('utf8');  
    
    • mysqli
        mysqli_set_charset('utf8');  
        $mysqli->set_charset('utf8');   
    
    • PDO
        $dbh = new PDO($name, $username, $password);
        $dbh->exec("set names utf8");