Datatables and json formatting error with php

HI im with little difficulty with dataTables and php. I repeat json in the following format:

{"iTotalRecords": 10, "iTotalDisplayRecords": 10, "aaData": [["1", "15", "1", "detailed description of the long description of the long description", "2012-02-25 00:00: 00 "], [" 1 "," 15 "," 1 "," long description long description long description long description "," 2012-02-25 00:18:59 "] ...]}

Which program works with my dataTable, however, after checking the above at jsonlint.com/, I got a well-formed version below:

   {
            "iTotalRecords": 10,
            "iTotalDisplayRecords": 10,
            "aaData": [
                [
                    "1",
                    "15",
                    "1",
                    "long description long description long description long description",
                    "2012-02-25 00:18:59"
                ],
        ...

            ]
        }

txt , . , " " . , json, php script, ? \n -, , , .

0
3

, , , json.

0

{ } . , , . :

php JSON:

$this->arrayJSON = $this->arrayPHPToJS($myArray);

public function arrayPHPToJS($phpArray) { 
    if (is_null($phpArray)) return 'null'; 
    if (is_string($phpArray)) return "'" . $phpArray . "'"; 
    if (self::is_assoc($phpArray)) { 
        $a=array(); 
        foreach ($phpArray as $key => $val ) 
            $a[]=self::arrayPHPtoJS($val); 
        return "[" . implode ( ', ', $a ) . "]"; 
    } 
    if (is_array($phpArray)) { 
        $a=array(); 
        foreach ($phpArray as $val ) 
            $a[]=self::arrayPHPtoJS($val); 
        return "[" . implode ( ', ', $a ) . "]"; 
    } 
    return json_encode($phpArray); 
}

JS script (, html-):

<script type="text/javascript">
    var jsArray = <?php echo $arrayJSON; ?>;

    $(function(){
        var oTable = $('#mytable').dataTable( {
            "aaData": jsArray,
            ....
        });
    });
</script>
+1

more like the number of columns configured in the / datatables table doesn't match the number of elements in your json array

0
source

Source: https://habr.com/ru/post/1794453/


All Articles