Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Datatables i problem z polskimi znakami
Forum PHP.pl > Forum > Przedszkole
trinq
Witam serdecznie, zarejestrowałem się na forum, gdyż chciałbym poprosić o pomoc. Mam problem z wyświetlaniem tabeli korzystając z datatables z server-side. Problem polega na tym, że tabela nie wyświetla się i zgłasza błąd: Invalid JSON Response i występuje, kiedy w bazie danych mam zaimportowane dane z polskimi i niemieckimi znakami. Ustawienia bazy i tabeli wybrane są na kodowanie UTF-8. W momencie, kiedy wyczyściłem tabelę w phpmyadmin i ręcznie pousuwałem polskie i niemieckie znaki z pliku CSV, który następnie importuję (dokładnie ten sam plik, tyle że bez "ogonków") wszystko działa poprawnie, tabela wyświetla się bez żadnych błędów. Jeżeli macie na to jakiś pomysł to bardzo bym prosił o pomoc, bo zupełnie nie wiem już gdzie szukać przyczyny. Domyślam się jedynie, że JSON nie ogarnia ogonków i nie pobiera danych, być może trzeba gdzieś wymusić kodowanie utf-8? Zamieszczam zrzuty plików. Z góry dziękuję

index.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0">
  6.  
  7. <title></title>
  8. <link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css">
  9. <link rel="stylesheet" type="text/css" href="css/shCore.css">
  10. <link rel="stylesheet" type="text/css" href="css/demo.css">
  11. <style type="text/css" class="init">
  12.  
  13. th, td { white-space: nowrap; }
  14. div.dataTables_wrapper {
  15. width: 800px;
  16. margin: 0 auto;
  17. }
  18.  
  19. </style>
  20. <script type="text/javascript" language="javascript" src="js/jquery.js"></script>
  21. <script type="text/javascript" language="javascript" src="js/jquery.dataTables.js" charset="utf-8"></script>
  22. <script type="text/javascript" language="javascript" src="js/shCore.js"></script>
  23. <script type="text/javascript" language="javascript" src="js/demo.js"></script>
  24. <script type="text/javascript" language="javascript" class="init" charset="utf-8">
  25.  
  26. $(document).ready(function() {
  27. $('#datatables').dataTable( {
  28. "processing": true,
  29. "serverSide": true,
  30. "ajax": "js/server_processing.php"
  31. } );
  32. } );
  33.  
  34. </script>
  35. </head>
  36.  
  37. <body class="dt-example">
  38. <div class="container">
  39. <table id="datatables" class="display" cellspacing="0" width="100%">
  40. <thead>
  41. <tr>
  42. <th>Name</th>
  43. <th>Date 1</th>
  44. <th>Date 2</th>
  45. <th>Place</th>
  46. <th>Book number</th>
  47. <th>Location</th>
  48. <th>Status</th>
  49. <th>Country</th>
  50. <th>Religion</th>
  51. <th>Card ID</th>
  52. <th>Description</th>
  53. <th>Data 1</th>
  54. <th>Data 2</th>
  55. </tr>
  56. </thead>
  57.  
  58. </table>
  59. </div>
  60.  
  61. </body>
  62. </html>


server-processing.php

  1. <?php
  2.  
  3. /*
  4.  * DataTables example server-side processing script.
  5.  *
  6.  * Please note that this script is intentionally extremely simply to show how
  7.  * server-side processing can be implemented, and probably shouldn't be used as
  8.  * the basis for a large complex system. It is suitable for simple use cases as
  9.  * for learning.
  10.  *
  11.  * See <a href="http://datatables.net/usage/server-side" target="_blank">http://datatables.net/usage/server-side</a> for full details on the server-
  12.  * side processing requirements of DataTables.
  13.  *
  14.  * @license MIT - <a href="http://datatables.net/license_mit" target="_blank">http://datatables.net/license_mit</a>
  15.  */
  16.  
  17. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  18.  * Easy set variables
  19.  */
  20.  
  21. // DB table to use
  22. $table = 'datatables';
  23.  
  24. // Table's primary key
  25. $primaryKey = 'id';
  26.  
  27. // Array of database columns which should be read and sent back to DataTables.
  28. // The `db` parameter represents the column name in the database, while the `dt`
  29. // parameter represents the DataTables column identifier. In this case simple
  30. // indexes
  31. $columns = array(
  32. array( 'db' => 'name', 'dt' => 0 ),
  33. array( 'db' => 'date_1', 'dt' => 1 ),
  34. array( 'db' => 'date_2', 'dt' => 2 ),
  35. array( 'db' => 'place', 'dt' => 3 ),
  36. array( 'db' => 'book', 'dt' => 4 ),
  37. array( 'db' => 'location', 'dt' => 5 ),
  38. array( 'db' => 'status', 'dt' => 6 ),
  39. array( 'db' => 'country', 'dt' => 7 ),
  40. array( 'db' => 'rel', 'dt' => 8 ),
  41. array( 'db' => 'card_num', 'dt' => 9 ),
  42. array( 'db' => 'desc', 'dt' => 10 ),
  43. array( 'db' => 'data_1', 'dt' => 11 ),
  44. array( 'db' => 'data_2', 'dt' => 12 ),
  45. );
  46.  
  47. // SQL server connection information
  48. $sql_details = array(
  49. 'user' => 'root',
  50. 'pass' => 'psw',
  51. 'db' => 'datatables',
  52. 'host' => 'localhost'
  53. );
  54.  
  55.  
  56. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  57.  * If you just want to use the basic configuration for DataTables with PHP
  58.  * server-side, there is no need to edit below this line.
  59.  */
  60.  
  61. require( 'ssp.class.php' );
  62.  
  63. echo json_encode(
  64. SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
  65. );


46and2
spieprzyli klase ssp.class.php

w funkcji sql_connect musisz zmienic:

array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )

na:

array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' )

mało tego, że nie wyświetlały się polskie znaki, to niektóre pola z tabeli w ogóle były pomijane i wyświetlane jako null, ale po tej poprawce, jest już ok
greghome
Hej
Zrobiłem wszystko tak jak wyżej, polskie znaki wyświetlają sie prawidłowo (pod bazą mysql również są polskie znaki).
Jednak problem mam z polem "search" - Datatables.
Przy próbie filtrowania pojawia się komunikat:
"DataTables warning: table id=datatablemain - An SQL error occurred: SQLSTATE[HY000]: General error: 1271 Illegal mix of collations for operation 'like'"


Pozdrawiam i prosze o pomoc w znalezieniu przyczyny.
com
Poprosimy kod smile.gif
greghome
Hej,

Znalazłem przyczynę występowania błędu.
Okazuje sie, że maksymalna liczbę kolumn która można wyświetlić to liczba 37. Jak ustawiłem kodowanie "UTF8"- jak w powyzszym przykładzie to co prawda nadal maksymalna liczba kolumn którą miałem ustawioną wyświetlała się poprawnie, lecz przestało działać okno "search" (Dla polskich znaków - z ogonkami. Dla pozostałych jest ok ). Natomiast zmniejszenie liczby kolumn do 19 (oczywiście z kodowaniem "utf8") przywraca funkcjonalność okna "search" - działa poprawnie.
Ktoś może miał podobny problem? Jak można to poprawić. Chciałbym wyświetlać ok 40 kolumn.

Z tego co udało mi się doczytać jest to spowodowane metodą $_GET , ktos próbował $_POST ?

Pozdrawiam
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.