UTF8字符无法正确显示数据和yadcf [英] UTF8 characters not displaying properly with datatables and yadcf

查看:192
本文介绍了UTF8字符无法正确显示数据和yadcf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在UTF8中的mysql数据库,我已经正确输出了这些字母,但是不能让我的php使用ajax进程的数据表。我已经尝试过各种方法来获得UTF8来显示,包括更改标题,使用mysql函数,以及更改json编码功能,但是我无法使其工作。请帮助我正确显示UTF8?



我有:temps.php
我的服务器端处理脚本:

 <?php 
header(Content-Type:text / json; charset = utf-8);
require('ssp.class.php');
$ table ='articles';
$ primaryKey ='id';
$ columns = array(
array('db'=>'title','dt'=> 0),
array('db'=>'description' 'dt'=> 1));
$ sql_details = array(
'user'=>'root','pass'=>'pass','db'=>'test','host'=> localhost'
);

$ db = SSP :: db($ sql_details);
.....


if($ clause!==){
$ whereAll [] = $ clause;
$ _GET ['columns'] [$ i] ['search'] ['value'] =;
}}
echo json_encode(
SSP :: complex($ _GET,$ db,$ table,$ primaryKey,$ columns,null,
(!empty($ whereAll) ?implode(AND,$ whereAll):)
));

我有:temp.html:我的Javascript功能(基于datatables和yadcf)

  $(document).ready(function(){
$(#example)。dataTable({
response:true,
processing:true,
serverSide:true,
ajax:scripts / temps.php,
columns [
// ID
null,

//分销商
null
//导演

]
})。yadcf([
// ID
{
column_number:0,
filter_type:text,
filter_reset_button_text:false
},
//摘要
{
column_number:1,
filter_type:text,
filter_delay:500,
filter_reset_button_text:false
}


]);
});

我有temp.html输出数据:(编辑因为很多代码是不相关的问题)

 <!DOCTYPE html> 
< div>
< head>

< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< meta name =DescriptionCONTENT =/>
..........
< table id =exampleclass =table table-striped table-bordered table-condensedcellspacing =0width =100% >
< thead>
< tr>
< div>< div> One< / div>< / th>
< div>< div> Two< / div>< / th>

< / tr>
< / thead>
< / table>

对于这么长的问题,对不起,但是还有什么可以改变,使UTF8正确显示在html页面上?我已经尝试了所有可以想到的变量和函数的组合;但不能使其工作。也许有一个js函数可以应用于某个地方?感谢您的帮助。



编辑:SQL结构供参考:

  CREATE TABLE IF NOT EXISTS`articles`(
`id` int(11)NOT NULL,
`title` varchar(300)CHARACTER SET utf8 DEFAULT NULL,
`description` text CHARACTER SET utf8
)ENGINE = InnoDB AUTO_INCREMENT = 4 DEFAULT CHARSET = latin1;
- 转储表`articles`的数据
INSERT INTO`articles`(`id`,`title`,`description`)VALUES
(1,'శశశీీివససతతతతత'概念英语第asv'),
(2,'asda','asdవㄧ'),
3,'sdfsadfsdf英语','sadf英');


解决方案

您需要强制 utf8

  $ db = SSP :: db ($ sql_details); 
$ db-> exec(set names utf8);

或者,尝试将其作为参数传递:

  $ sql_details = array(
'user'=>'root',
'pass'=>'ryan ',
'db'=>'edata',
'host'=>'localhost',
'charset'=>'utf8'
);

但是这并不适用于所有PHP版本。



PS:为什么将表格字段设置为类型为 utf8 ,但表格字符集设置为 latin1


I have a mysql database that is in UTF8, I've properly output the letters before but cannot make my php do so with datatables using the ajax process. I have tried various methods to get the UTF8 to show, including, changing headers, using mysql functions, and changing the json encode function, but I cannot make it work. Please help me show UTF8 properly instead of ?s.

I have: temps.php My server side processing script:

 <?php
 header("Content-Type: text/json; charset=utf-8");
 require( 'ssp.class.php' );
 $table = 'articles';
 $primaryKey = 'id';
 $columns = array(
      array( 'db' => 'title',           'dt' => 0  ),
    array( 'db' => 'description',  'dt' => 1  ));
 $sql_details = array(
 'user' => 'root', 'pass' => 'pass', 'db'   => 'test', 'host' =>      'localhost'
 );

 $db = SSP::db($sql_details);
.....


 if($clause !== ""){
  $whereAll[] = $clause;
  $_GET['columns'][$i]['search']['value'] = "";
 }}
 echo json_encode(
 SSP::complex( $_GET, $db, $table, $primaryKey, $columns, null, 
  (!empty($whereAll) ? implode(" AND ", $whereAll) : "")
  ));

I have: temp.html: My Javascript function (based on datatables and yadcf)

$(document).ready(function(){
$("#example").dataTable({
    "responsive": true,
    "processing": true,
    "serverSide": true,
    "ajax": "scripts/temps.php",
    "columns": [
       // ID
       null,

       // Distributor
       null
       // Director

    ]
}).yadcf([
    // ID
    {
      column_number: 0 ,
      filter_type: "text",
      filter_reset_button_text: false
    },
    // Abstract
    {
      column_number: 1,
      filter_type: "text",
      filter_delay: 500,
      filter_reset_button_text: false
    },


]);
});

And I have temp.html where the data is outputted: (redacted because a lot of the code is irrelevant to the question)

 <!DOCTYPE html>
 <div>
 <head>

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <meta name="Description" CONTENT=""/>
     ..........
 <table id="example" class="table table-striped table-bordered table-condensed" cellspacing="0" width="100%">
  <thead>
     <tr>
        <th><div>One</div></th>
        <th><div>Two</div></th>

     </tr>
  </thead>
 </table>

Sorry for such a long question; but what else can I change to make UTF8 properly show on the html page? I've tried all the combinations of variables and functions I can think of; but can't make it work. Perhaps there is a js function that could be applied somewhere? Thank you for the help.

Edit: SQL structure for reference:

     CREATE TABLE IF NOT EXISTS `articles` (
    `id` int(11) NOT NULL,
    `title` varchar(300) CHARACTER SET utf8 DEFAULT NULL,
    `description` text CHARACTER SET utf8
 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
 -- Dumping data for table `articles`
 INSERT INTO `articles` (`id`, `title`, `description`) VALUES
 (1, 'శ్రీనివాస్scddadas తామాడా', '新概念英语第asv'),
 (2, 'asda', 'asdవా'),
 3, 'sdfsadfsdf英语', 'sadf英');

解决方案

You need to force utf8 in the PDO connection :

$db = SSP::db($sql_details);
$db->exec("set names utf8");

alternatively, try pass it as a param :

$sql_details = array(
  'user' => 'root', 
  'pass' => 'ryan', 
  'db'   => 'edata', 
  'host' => 'localhost', 
  'charset' => 'utf8' 
);

But this does not work with all PHP versions.

PS: Why do you set the table fields to be of type utf8, but the table character set to be latin1?

这篇关于UTF8字符无法正确显示数据和yadcf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆