0

Finally i could get working these DataTables. But now i tried to add one more column and now it is no longer working. Why is this?

With 7 columns it is working fine. Now with 8 it just didn't work and i get that error.

For 7 columns here is the whole working code:

PHP code:

$loteria='7column';//it doesn't work for '8columns'
$lotto = new Lotto();
$ultimos_resultados=$lotto->last_results($loteria,$sorteos);

function objectToArray($d) 
{
    if (is_object($d)) {
        $d = get_object_vars($d);
    }

    if (is_array($d)) {
        return array_map(__FUNCTION__, $d);
    } else {
        // Return array
        return $d;
    }
}

$new_array = objectToArray($ultimos_resultados);
//echo '<pre>',print_r($new_array),'</pre>';

$result = array();
echo '[';
foreach ($new_array as $new_array2) {
    echo '[';
    foreach ($new_array2 AS $value){
        if (1 == strlen($value)) {
            $zero=0;
            $value = '"'.$zero.$value.'"';
        }
        echo $value;
        if($value!==end($new_array2)){ //referencias: http://stackoverflow.com/a/8780881/1883256
            echo',';
        }
    }
    echo ']';//referencias: http://www.mydigitallife.info/how-to-access-php-array-and-multidimensional-nested-arrays-code-syntax/
    if($new_array2!==end($new_array)){
        echo ',';
    }else{ echo '';}
}
echo ']';

This is the 7-column php script output array:

[[2753,17,26,28,31,37,47],[2754,"08",10,23,26,44,56],[2755,"04",12,16,20,22,47],[2756,12,19,33,34,41,55],[2757,32,34,35,36,50,55]]

jQuery code:

$(document).ready(function() {

$.ajax({
    url: "ajax/default_chart_numbers_table.php",
    type: "post",
    data: {loteria: '7-column', sorteos: 5}, /*Number of rows*/ 
    success : function (resp){
        // would look something like ['val1','val2', 'etc']
        var column_data = $.parseJSON(resp);
        alert(column_data);
        //alert('The element of array number [2] is:' + column_data[2]);        
        // adding data to datatables
        // if column_data is 1 row
        //$('#dataTables-melate').dataTable().fnAddData(column_data);
        for (var j=0;j<=column_data.length-1;j++){
            // adding each row with its column data
            //iterating values to add leading zeros to single digits:            
            $('#dataTables-melate').dataTable().fnAddData(column_data[j]);
        }
    },
    error: function(jqXHR, textStatus, ex) {
      console.log(textStatus + "," + ex + "," + jqXHR.responseText);
    }
});    

} );

HTML DataTables code:

<table id="dataTables-melate" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                                <thead>
                                    <tr>
                                        <th>Concurso</th>
                                        <th>R1</th>
                                        <th>R2</th>
                                        <th>R3</th>
                                        <th>R4</th>
                                        <th>R5</th>
                                        <th>R6</th>
                                        <!--<th>R7</th> THIS IS FOR 8th column-->
                                    </tr>
                                </thead>

                                <tfoot>
                                    <tr>
                                        <th>Concurso</th>
                                        <th>R1</th>
                                        <th>R2</th>
                                        <th>R3</th>
                                        <th>R4</th>
                                        <th>R5</th>
                                        <th>R6</th>
                                        <!--<th>R7</th> THIS IS FOR 8th column-->
                                    </tr>
                                </tfoot>
                            </table>

Until here everything is fine ... but when the php script output array has 8 columns like this:

[[2753,"08",16,21,39,50,52,"04",],[2754,11,18,31,35,39,42,34],[2755,"04",20,29,31,44,48,49],[2756,"05","06",33,34,46,55,38],[2757,"06",18,36,48,50,52,28]]

then it no longer works and here i get the following error:

SyntaxError: JSON.parse: unexpected character at line 1 column 33 of the JSON data

Of course, i add one more column at the HTML code.

I don't know where the bug is. How do i fix this?

Pathros
  • 10,042
  • 20
  • 90
  • 156

1 Answers1

2

Just before the 33rd character (in the second data set) you have an extra comma, here:

[[2753,"08",16,21,39,50,52,"04", <<<
Andy G
  • 19,232
  • 5
  • 47
  • 69