最后我可以得到这些数据表工作。但现在我试图增加一列,现在它不再工作。为什么会这样?
有7列它工作得很好。现在有了8,它就不工作了,我得到了那个错误。
对于7列,下面是完整的工作代码:
PHP代码:$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 ']';
这是7列php脚本输出数组:
[[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代码:$(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数据表代码:
<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>
在此之前一切都很好…但是当PHP脚本输出数组有8列时,像这样:
[[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]]
然后它不再工作,这里我得到以下错误:
SyntaxError: JSON。解析:JSON数据第1行第33列的意外字符
当然,我在HTML代码中增加了一列。
我不知道bug在哪里。我怎么解决这个问题?
在第33个字符之前(在第二个数据集中)有一个额外的逗号,在这里:
[[2753,"08",16,21,39,50,52,"04", <<<