PHP jQuery JSON format - php
I have the following code in PHP which returns this JSON code. I can't return the values for "OK" because of the format, I must do some jQuery trick to get it, but I don't want it... Some advice will be appreciated.
PHP
while($row = sqlsrv_fetch_array($datBS)) {
$ahora = $row['servicio'];
if($ahora != $antes && $n == 1) {
$ok = array();$ko = array(); $rt = array(); $horas = array();
}
if($row['peticion_id'] == 0) {$ok[] = round($row['valor'], 3); $horas[] = $row['hora'];}
if($row['peticion_id'] == 1) $ko[] = round($row['valor'], 3);
if($row['peticion_id'] == 2) $rt[] = round($row['valor'], 3);
$datosBS[$ahora] = array(
"OK" => $ok,
"KO" => $ko,
"RT" => $rt,
"HORAS" => $horas
);
$antes = $ahora;
$n = 1;
}
while($row = sqlsrv_fetch_array($sqlTotalsBS)) {
$bs[$row['servicio']] = array(
"SUMOK" => intval($row["OK"]),
"SUMKO" => intval($row["KO"])
);
}
$banksphere = array_merge_recursive((array)$bs, (array)$datosBS);
$json = array_merge((array)$banksphere);
echo json_encode($json);
JSON
{"Servicing":{"SUMOK":923391,"SUMKO":1048,"OK":[184,69,28,14,15,15,0,11,13,0,14,21,19,3,8,0,5,17,13,0,8,30,5,3,13,18,26,24,46,116,342,790,2828,9795,15647,21394,23710,26214,27522,27038,26603,28939,29149,29222,28020,30061,29967,20139,21436,31416,31354,32472,32659,32435,33767,33623,33394,27204,28830,32562,34844,20197,11903,6923,6855,6133,6051,7456,7842,8366,9271,10127,10301,9845,9616,8391],"KO":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,4,9,17,20,11,29,30,35,65,89,56,52,59,26,35,70,39,39,60,28,15,12,27,20,37,20,20,12,8,5,3,8,10,5,12,4,11,9,9,10,11,4],"RT":[0.139,0.171,0.73,0.187,3.667,3.126,0,3.629,7.227,0,4.279,5.967,3.195,2.862,0.883,0,5.441,6.495,0.883,0,1.835,1.656,2.09,0.111,0.35,1.015,1.457,0.829,0.635,0.767,0.534,0.325,0.202,0.172,0.142,0.129,0.125,0.127,0.123,0.125,0.124,0.12,0.122,0.116,0.121,0.114,0.116,0.115,0.116,0.127,0.118,0.128,0.123,0.119,0.12,0.115,0.113,0.114,0.119,0.11,0.104,0.142,0.112,0.139,0.107,0.131,0.149,0.139,0.139,0.133,0.131,0.116,0.109,0.122,0.116,0.113],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Estructurales":{"SUMOK":58088,"SUMKO":453,"OK":[25,12,10,2,16,12,5,11,4,6,10,14,11,7,10,4,7,16,9,6,10,13,9,4,14,10,14,20,33,81,144,366,1562,5956,3671,2251,1976,1960,1600,1656,1475,1396,1473,1567,1412,1486,1553,1198,1046,1655,1636,1743,1711,1550,1417,1340,1562,1312,993,1285,925,790,377,311,286,324,378,486,602,461,542,491,474,450,401,433],"KO":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,2,5,11,11,14,10,9,17,10,9,21,10,10,5,8,14,18,16,33,22,18,19,27,21,29,16,13,11,3,2,1,5,5,1,2,3,3,3,2,4,7],"RT":[0.072,0.888,0.085,0.055,0.063,0.065,0.044,0.113,0.046,0.139,0.08,0.215,0.089,0.134,0.083,0.51,0.242,0.224,0.255,0.126,0.125,0.206,0.343,0.062,0.06,0.229,0.074,0.334,0.08,0.186,0.098,0.113,0.141,0.056,0.091,0.165,0.189,0.187,0.211,0.288,0.248,0.267,0.263,0.279,0.296,0.263,0.257,0.266,0.269,0.303,0.294,0.274,0.275,0.284,0.289,0.287,0.297,0.256,0.591,0.25,0.31,0.382,0.25,0.265,0.1,0.115,0.125,0.123,0.132,0.561,0.172,0.254,0.14,0.106,0.193,0.187],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Movilidad":{"SUMOK":96919,"SUMKO":27,"OK":[1563,1396,1250,1082,825,652,427,416,305,266,205,233,149,203,141,118,148,132,101,86,111,141,205,136,285,405,445,584,736,1159,1047,1333,1406,1627,1828,1978,2393,2533,2351,2445,2182,2346,2068,2067,1900,2187,2161,1380,1093,1891,2060,1877,1825,1806,1896,1854,1748,1613,1789,1795,1939,1963,2123,1704,1864,1693,1862,1706,1461,1488,1608,1367,1419,1516,1347,1475],"KO":[1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,1,1,0,0,0,0,1,0,2,1,0,0,1,0,2,0,1,2,2,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,4],"RT":[0.361,0.462,0.42,0.417,0.379,0.583,0.43,0.377,0.449,0.505,0.428,0.591,0.466,0.459,0.595,0.441,0.481,0.462,0.65,0.674,0.668,0.519,0.562,0.547,0.52,0.546,0.474,0.481,0.484,0.457,0.449,0.503,0.432,0.425,0.438,0.422,0.441,0.426,0.395,0.397,0.4,0.385,0.408,0.469,0.416,0.45,0.386,0.414,0.413,0.413,0.395,0.373,0.418,0.386,0.387,0.376,0.386,0.398,0.405,0.406,0.399,0.407,0.395,0.385,0.421,0.397,0.372,0.351,0.39,0.384,0.359,0.401,0.403,0.435,0.395,0.355],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Riesgos":{"SUMOK":200875,"SUMKO":1409,"OK":[50,61,31,55,28,26,5,60,20,0,15,0,15,15,0,0,15,0,15,15,0,15,15,0,13,2,0,30,2,44,146,268,510,1265,2425,2955,3841,4581,5321,5587,5944,6327,5300,5337,5291,5679,5805,3869,3708,6929,7262,6740,6493,6381,6789,6080,5504,6053,5190,5224,4327,2092,1426,1499,2065,2972,3495,3766,4504,4265,4429,5289,4926,4524,3760,4185],"KO":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,5,5,7,5,17,18,24,24,53,36,47,40,41,43,24,26,53,54,62,58,41,46,51,33,29,26,18,10,13,14,16,21,23,48,53,45,49,34,34,43,38,36,43],"RT":[0.065,0.083,0.13,0.707,0.099,0.084,2.82,0.194,0.22,0,0.492,0,0.433,0.161,0,0,0.298,0,0.537,0.446,0,0.52,0.728,0,0.287,0.345,0,0.082,1.316,1.136,0.352,0.576,0.38,0.27,0.256,0.176,0.179,0.136,0.143,0.152,0.126,0.144,0.13,0.154,0.139,0.151,0.134,0.142,0.153,0.166,0.156,0.147,0.137,0.141,0.129,0.142,0.148,0.132,0.128,0.18,0.124,0.159,0.183,0.154,0.147,0.128,0.135,0.141,0.131,0.175,0.141,0.114,0.14,0.139,0.172,0.162],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Particulares":{"SUMOK":709540,"SUMKO":534,"OK":[5794,5284,4816,3580,3018,2283,2006,1486,1186,1311,918,794,607,677,671,600,412,584,454,506,457,672,665,649,932,894,1412,1727,2158,3063,4326,5923,7038,10129,13647,14923,15905,19339,21463,22762,21936,22069,21976,21072,19951,19413,19256,13127,12318,18637,17980,18297,17357,16708,16401,15354,14515,12789,11771,10628,11232,10458,11007,11007,11753,11383,11922,11604,11120,10776,11697,11471,11463,12479,11531,12011],"KO":[7,8,3,1,7,4,3,1,2,4,2,1,3,0,3,0,0,0,1,1,0,0,2,0,0,0,0,0,0,4,1,5,4,5,7,6,5,5,11,12,17,14,12,13,20,13,12,9,8,21,13,12,15,17,7,13,11,6,9,9,5,12,11,8,10,8,13,8,8,11,7,8,13,20,11,12],"RT":[0.197,0.241,0.204,0.205,0.247,0.241,0.315,0.302,0.178,0.433,0.505,0.181,0.191,0.183,0.228,0.226,0.219,0.326,0.337,0.535,0.273,0.229,0.267,0.218,0.223,0.202,0.219,0.212,0.241,0.191,0.187,0.169,0.2,0.152,0.158,0.171,0.14,0.159,0.149,0.143,0.156,0.146,0.145,0.137,0.141,0.159,0.142,0.158,0.149,0.136,0.133,0.124,0.16,0.145,0.141,0.139,0.143,0.149,0.126,0.132,0.163,0.192,0.181,0.172,0.186,0.147,0.132,0.167,0.138,0.18,0.153,0.146,0.143,0.166,0.142,0.144],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Particulares BKS3":{"SUMOK":799461,"SUMKO":731,"OK":[6917,6108,5726,4223,3433,2683,2391,1693,1304,1253,921,800,688,693,715,634,493,505,426,418,446,743,735,691,1073,984,1672,1881,2260,3230,4607,6421,7692,11239,14628,16550,18563,20975,23237,25076,24469,24579,24123,23419,22116,21400,21976,14099,13843,21162,20687,21039,19535,18668,18566,17750,16300,14561,13380,12424,12500,11909,12463,12460,13241,12829,13668,13038,12602,12539,13516,13739,13601,14415,13847,14271],"KO":[10,6,8,2,2,2,2,7,1,1,0,0,4,0,0,1,0,0,1,0,0,0,0,0,0,0,5,5,3,6,4,8,6,7,7,7,10,14,19,16,17,16,12,18,17,28,23,15,18,18,28,21,23,19,13,11,16,9,15,11,17,10,8,15,23,17,15,8,7,13,15,12,10,18,18,13],"RT":[0.152,0.197,0.156,0.153,0.151,0.199,0.201,0.144,0.149,0.198,0.176,0.34,0.445,0.346,0.406,0.522,0.505,0.405,0.458,0.363,0.297,0.349,0.345,0.216,0.218,0.193,0.2,0.202,0.261,0.525,0.225,0.266,0.17,0.153,0.146,0.148,0.148,0.15,0.154,0.151,0.152,0.155,0.151,0.152,0.142,0.147,0.143,0.142,0.143,0.152,0.148,0.148,0.147,0.151,0.149,0.147,0.141,0.141,0.14,0.144,0.138,0.152,0.141,0.145,0.142,0.144,0.136,0.135,0.136,0.138,0.14,0.144,0.138,0.143,0.143,0.143],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Empresas":{"SUMOK":183051,"SUMKO":658,"OK":[405,389,293,222,254,175,150,182,129,85,78,96,71,85,78,70,70,77,121,77,74,76,102,94,138,149,202,205,371,392,662,958,1269,2478,3425,4178,4458,5982,6565,6905,6723,6242,5983,5989,6005,5467,5452,3187,3855,6037,4980,5874,5682,5283,5166,4975,3949,3239,2753,2354,1884,1819,2065,2305,2645,2640,2954,2835,3068,3017,3068,2997,2682,2849,2599,2639],"KO":[5,0,1,0,6,1,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,3,0,0,0,1,5,7,12,9,13,9,15,21,22,22,21,17,24,13,17,20,13,17,23,21,13,22,10,13,19,15,6,11,9,8,5,9,7,21,19,9,5,14,27,7,18,10,13,11,15],"RT":[0.145,0.43,0.128,0.141,0.13,0.138,0.087,0.105,0.093,0.219,0.073,0.203,0.158,0.117,0.178,0.109,0.247,0.259,0.472,0.261,0.375,0.159,0.163,0.21,0.252,0.201,0.222,0.707,0.72,0.303,0.341,0.46,0.195,0.132,0.11,0.102,0.104,0.116,0.121,0.102,0.122,0.128,0.131,0.129,0.105,0.102,0.111,0.111,0.11,0.12,0.101,0.112,0.108,0.106,0.112,0.098,0.105,0.141,0.113,0.096,0.089,0.099,0.114,0.12,0.105,0.112,0.1,0.093,0.099,0.138,0.103,0.095,0.108,0.106,0.112,0.098],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Juzgados":{"SUMOK":333138,"SUMKO":102,"OK":[87,59,33,49,59,73,45,63,67,39,46,31,41,58,21,53,32,31,45,30,41,37,21,68,44,138,211,174,47,62,110,393,710,1359,2868,3914,5416,7705,9541,10509,11443,11403,11296,11986,12718,13056,12457,7840,8102,12545,14057,14422,13735,14547,14024,14292,13172,14054,12174,9538,8052,5539,4256,3502,2772,2498,2164,1823,1420,1154,1533,1671,1602,1420,1399,1142],"KO":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,2,0,7,12,13,13,1,2,6,4,3,13,1,8,4,2,0,0,0,1,1,4,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],"RT":[0.304,0.211,0.108,0.096,0.119,0.101,0.071,0.088,0.085,0.146,0.098,0.093,0.141,0.087,0.082,0.134,0.488,0.141,0.294,0.413,0.116,0.109,0.091,0.191,0.082,0.142,0.104,0.086,0.088,0.149,0.142,0.15,0.146,0.143,0.113,0.114,0.113,0.11,0.11,0.113,0.15,0.176,0.17,0.178,0.118,0.123,0.12,0.129,0.135,0.162,0.111,0.12,0.13,0.111,0.104,0.107,0.105,0.105,0.104,0.102,0.102,0.107,0.097,0.1,0.093,0.112,0.083,0.111,0.086,0.104,0.105,0.114,0.118,0.095,0.115,0.126],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Broker":{"SUMOK":28224,"SUMKO":84,"OK":[71,74,68,78,32,52,39,17,16,11,16,19,1,9,21,17,6,8,1,6,2,69,41,36,59,34,49,37,16,28,42,18,57,75,70,250,413,1222,987,1045,1109,1110,919,926,873,689,876,426,399,529,576,494,531,462,716,681,527,498,475,386,541,619,451,656,608,831,734,791,715,794,1134,1060,334,242,175,225],"KO":[2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1,0,0,0,0,0,0,0,0,0,1,0,0,1,1,2,9,5,0,2,5,0,1,0,0,4,4,2,1,1,5,1,2,3,0,3,3,1,1,0,3,5,2,3,2,1,0,0,0,3,0],"RT":[0.293,0.716,9.363,0.348,0.363,0.272,0.262,0.213,0.397,1.622,1.954,0.391,4.974,0.728,0.362,1.751,0.501,1.809,0.606,1.114,4.113,0.554,0.418,0.332,0.487,0.244,0.544,0.214,0.23,0.295,0.336,0.242,0.457,0.321,0.457,0.452,0.383,0.439,0.431,0.44,0.469,0.448,0.508,0.463,0.398,0.555,0.46,0.362,0.408,0.426,0.368,0.313,0.316,0.263,0.334,0.318,0.298,0.324,0.493,0.301,0.383,0.345,0.34,0.445,0.313,0.38,0.348,0.324,0.402,0.401,0.476,0.41,0.224,0.22,0.184,0.32],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Sistemas Informacion":{"SUMOK":84657,"SUMKO":282,"OK":[19,19,11,0,6,3,0,6,0,0,6,0,3,3,3,0,3,3,3,3,0,6,3,0,3,6,0,10,70,263,420,872,2529,7961,7513,5454,4559,3310,2970,2652,2740,2444,1807,1600,1960,1825,1666,1188,1299,1429,1684,1668,1291,1213,1438,1347,1324,1385,1355,1377,1344,618,450,347,449,690,972,1157,1200,872,1099,972,1049,969,824,913],"KO":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,3,0,2,4,0,1,6,6,0,1,1,12,0,5,3,4,1,22,3,2,4,0,1,0,0,24,101,0,0,2,2,0,1,0,0,2,1,1,33,29,3,1],"RT":[0.437,0.251,0.219,0,2.79,0.3,0,1.323,0,0,1.056,0,1.174,0.659,4.83,0,4.483,4.197,1.64,3.05,0,2.981,0.496,0,0.408,1.601,0,0.662,1.121,1.179,0.325,0.382,0.186,0.222,0.153,0.132,0.133,0.168,0.151,0.15,0.169,0.231,0.16,0.154,0.167,0.182,0.167,0.157,0.152,0.16,0.151,0.192,0.175,0.153,0.199,0.156,0.165,0.158,0.149,0.15,0.136,0.159,0.181,0.205,0.226,0.22,0.253,0.248,0.171,0.212,0.141,0.135,0.153,0.153,0.139,0.152],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Contact Center":{"SUMOK":158998,"SUMKO":216,"OK":[173,44,9,38,63,0,47,6,0,89,19,46,60,11,5,51,2,53,41,13,45,46,11,0,254,65,51,44,9,14,67,52,159,1421,1744,2741,3682,3418,7080,4342,3749,5957,3858,5085,4840,5412,5017,3080,3838,5769,5799,5220,4719,4668,4535,3613,4538,4016,3403,3951,3735,3245,2529,2366,2708,2495,2322,2336,2405,2249,2311,4426,2360,1910,2406,2113],"KO":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,4,2,4,2,4,4,7,4,5,15,12,8,3,5,13,14,12,3,5,7,2,9,5,3,4,0,4,6,1,0,3,8,2,1,7,4,2,3,7,4,4],"RT":[0.083,0.128,0.035,0.131,0.085,0,0.174,0.04,0,0.135,0.035,0.134,0.104,0.061,0.642,0.478,0.046,0.135,0.519,0.524,0.129,0.524,0.256,0,0.255,0.08,0.211,0.145,0.066,0.25,0.092,0.146,0.197,0.181,0.176,0.128,0.15,0.134,0.109,0.128,0.113,0.116,0.12,0.114,0.109,0.112,0.101,0.114,0.102,0.181,0.124,0.119,0.101,0.102,0.108,0.106,0.132,0.105,0.111,0.107,0.104,0.1,0.113,0.09,0.091,0.098,0.094,0.098,0.098,0.103,0.085,0.091,0.102,0.084,0.102,0.087],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Cuentas Personales":{"SUMOK":110273,"SUMKO":73,"OK":[173,135,103,98,141,136,150,102,152,136,108,121,164,101,159,137,130,92,128,102,128,141,103,172,134,134,122,122,122,182,160,244,257,345,498,1170,1843,2355,2500,2503,2646,3045,3609,3141,3232,3720,3167,1809,2320,3944,3653,4718,3772,4470,5061,4953,5029,4992,5265,4342,4523,3027,1672,779,373,548,397,476,557,829,1028,519,420,1041,844,549],"KO":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,3,1,4,1,2,1,0,5,0,1,2,4,0,2,1,0,5,2,7,1,2,6,2,3,1,0,2,0,1,2,0,4,0,0,0,1,1,1],"RT":[0.419,0.439,0.481,2.572,0.854,0.831,0.547,0.426,0.424,1.022,1.369,0.541,0.523,0.528,0.596,0.5,0.563,1.319,2.444,0.764,2.516,0.506,0.478,0.431,0.452,0.437,0.407,0.482,0.415,0.453,0.95,0.845,0.51,1.378,0.528,0.436,0.486,0.357,0.454,0.45,0.373,0.363,0.421,0.401,0.36,0.388,0.414,0.399,0.375,0.388,0.387,0.393,0.383,0.372,0.395,0.394,0.426,0.421,0.517,0.398,0.478,0.581,0.372,0.463,0.346,0.423,0.436,0.429,0.306,0.41,0.336,0.483,0.379,0.365,0.408,0.412],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"Intervinientes":{"SUMOK":6490,"SUMKO":6,"OK":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,79,53,1,251,162,173,265,303,205,342,306,241,482,208,124,170,360,56,294,287,276,354,320,97,140,208,207,114,17,38,4,10,23,6,18,47,18,153,0,16,15,4],"KO":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0],"RT":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4.772,1.275,1.027,0.304,0.59,0.327,0.484,0.404,0.361,0.657,0.278,0.264,0.283,0.3,0.284,0.341,0.438,0.283,0.4,0.258,0.312,0.365,0.396,0.336,0.363,1.252,0.377,0.311,0.251,0.827,0.927,2.164,0.388,1.25,0.277,1.239,0.253,1.013,0.639,0,1.085,0.283,0.035],"HORAS":["00:00","00:15","00:30","00:45","01:00","01:15","01:30","01:45","02:00","02:15","02:30","02:45","03:00","03:15","03:30","03:45","04:00","04:15","04:30","04:45","05:00","05:15","05:30","05:45","06:00","06:15","06:30","06:45","07:00","07:15","07:30","07:45","08:00","08:15","08:30","08:45","09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30","11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45","15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00","18:15","18:30","18:45"]},"T4B":{"OK":142975,"KO":2293},"PCAS":{"OK":176,"KO":20}}
jQuery Trick
var OK = [];
$.each(value.OK, function(tipo, valor) {
OK = valor;
});
// Now if i return OK, it returns me the values... Same for OK, RT ...
My question is, I'm missing something on PHP? Because Do the trick on every $datosBS key is a code mess...
I think the problem is that "OK" for example, has subarray with the numbers, and JSON doesn't recognize it like "SUMOK" for example, which is plain number instead an array...
FULL JQUERY CODE
$.getJSON('test.php?entidad_id=2', function(data) {
$.each(data, function(key, value) {
var pies = {
name: key,
type: 'pie',
data: [],
center: [100, 80],
size: 100,
showInLegend: false,
dataLabels: {
enabled: false
}
};
var lineas = {
name: "Valores",
data: []
};
var renderId = "pie-" + key;
$('#charts').append('<div id="'+ renderId +'" class="pieChart"></div>');
lineas.data.push(value.OK); // <------ THIS ISN'T WORKING, EVEN IF I DO ([value.OK])
var OK = [];
$.each(value.OK, function(tipo, valor) {
OK = valor;
});
lineas.data.push(OK); // <----- THIS IS WORKING
pies.data.push(["OK", value.SUMOK], ["KO", value.SUMKO]);
if(key == "CAM") {
pies.data.push(["REJ_OTHERS", value.REJ_OTHERS]);
pies.data.push(["REJ_FORMAT", value.REJ_FORMAT]);
pies.data.push(["PENDING", value.PENDING]);
}
options.series.push(pies, lineas);
options.chart.renderTo = renderId;
options.title.text = key;
var chart = new Highcharts.Chart(options);
pies.data = null; lineas.data = null;
});
});
OK, data in line charts is 2 values [x, y] so the data array is like: [[x1, y1], [x2, y2]], [[1, 2, 3, 4, 5]] doesn't really make sense on a line chart.
If you just want to have the index across the x axis and the values along the y axis then you can build your array like:
$.each(value.OK, function(i, v) {
lineas.data.push([i, v]);
});
and just get rid of:
lineas.data.push(value.OK); // <------ THIS ISN'T WORKING, EVEN IF I DO ([value.OK])
var OK = [];
$.each(value.OK, function(tipo, valor) {
OK = valor;
});
lineas.data.push(OK); // <----- THIS IS WORKING
But without knowing what each data point in OK corresponds to, that is the best I got.
Here is a jsFiddle of some working code.
Hope this helps!
Edit
Looks like HORAS may be the time that corrosponds to the OK element, if that is the case you could do:
$.each(value.OK, function(i, v) {
lineas.data.push([value.HORAS[i], v]);
});
Related
how to use array with subarrays and json_encode to load part of the data
I have the below data in array with two sub-arrays 0 and 1. I am trying to load only sub-array data 0 or 1 every time via json_encode: My php code in my website: <?php $dataPoints_temp = array(array(),array()); $row = 1; if (($handle = fopen($latestFile, "r")) !== FALSE) { $data = array(); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); list($id, $time, $room, $pressure, $humidity, $pm2m5, $pm1m0) = $data; if ($id == "client1") { array_push($dataPoints_temp[0], array("x" => $data[0], "y" => $data[2])); } else if ($id == "client2") { array_push($dataPoints_temp[1], array("x" => $data[0], "y" => $data[2])); } } } ?> My javascript in my website does now the following: <script> window.onload = function() { var updateInterval = 1500; var $id = 0; var $dataPoints1 = <?php echo json_encode($dataPoints_temp[0], JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK); ?>; ... } </script> This works and I do get my graph working. The think that I am trying now is to make the $dataPoints_temp[0] more general to be able to select 0 or 1 as data points for the graph. var $dataPoints1 = <?php echo json_encode($dataPoints_temp[$id], JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK); ?>; But this gives me a lot of errors in the console of the browser although the $id is defined and for the moment set to 0. Try suggestion from #Wimanicesir, I am storing all the data in one php array and use the json_encode to store this to a javascript array and then trying to use JSON.parse for this array, but I am getting an error, see. client data: (5) […] 0: {…} hum: 38.31 id: "51df" pm1m0: 1 pm2m5: 1 pres: 1008.25 temp: 22.24 <prototype>: Object { … } 1: Object { id: "51df", temp: 22.24, pres: 1008.26, … } ... See code: var $rawData = <?php echo json_encode($client, JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK); ?>; var $obj = JSON.parse($rawData); Console error: SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data Two more tries: $client value is as expected from php json_encode: var $test1 = [{"hum": 38.31, "id": "51df", "pm1m0": 1, "pm2m5": 1, "pres": 1008.25, "temp": 22.24}]; $client value represents the data as string: var $test1 = '{"hum": 38.31, "id": "51df", "pm1m0": 1, "pm2m5": 1, "pres": 1008.25, "temp": 22.24}'; The rest of the test code: var $test2 = JSON.parse($test1); console.log($test2); In the above two cases only the at the 2nd point the JSON.parse is successful! Does this mean that JSON.parse needs a string as input? Update after a further investigation: var $rawData = <?php echo json_encode($client, JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK); ?>; var $json = JSON.stringify($rawData); var $dataPoints = JSON.parse($json); const clients = [... new Set($dataPoints.map(data => data.id))]; // find all unique clients id's, which are two in the sample data '51df' & '51ff' var $dataPoints1 = []; var $dataPoints2 = []; var $dataPointsHum = []; var $dataPointsP2m5 = []; var $dataPointsP1m0 = []; var $clientId = 0; // we want the data for client 0 for (i = 0; i < $dataPoints.length; i++) { if (clients[$clientId] == $dataPoints[i].id) { // load data only for client based on $clientId var tuple1 = {x: $dataPoints[i].id, y: $dataPoints[i].temp}; $dataPoints1.push(tuple1); var tuple2 = {x: $dataPoints[i].id, y: $dataPoints[i].pres}; $dataPoints2.push(tuple2); var tuple_hum = {x: $dataPoints[i].id, y: $dataPoints[i].hum}; $dataPointsHum.push(tuple_hum); var tuple_p2m5 = {x: $dataPoints[i].id, y: $dataPoints[i].pm2m5}; $dataPointsP2m5.push(tuple_p2m5); var tuple_p1m0 = {x: $dataPoints[i].id, y: $dataPoints[i].pm1m0}; $dataPointsP1m0.push(tuple_p1m0); } } The above code seems to works fine tested on a sample of data with two clients. How can I do that? Thanks
jQuery Ajax jSon set value from database based on condition on loop
I have jQuery json to get data from database. var list = ["Imsak", "Subuh", "Terbit", "Dhuhur", "Ashar", "Maghrib", "Isya"]; geo = [jsonStr.longitude, jsonStr.latitude, jsonStr.altitude]; timezone = jsonStr.timezone; prayTimes.tune({ imsak: 2, subuh: 2, terbit: -2, dhuhur: 121, ashar: 2, maghrib: 2, isya: 2 }); var date = new Date(); // today prayTimes.setMethod(jsonStr.calculationMethod); var times = prayTimes.getTimes(date, geo, timezone); $.ajax( { url: "chk", type: "POST", data: { }, dataType: "JSON", success: function (jsonStr) { var sholatNameLen = jsonStr.sholatName.length; for(var i=0; i<sholatNameLen; i++) { var sholatName = jsonStr.sholatName[i]; if(jsonStr.time10 == times[list[i].toLowerCase()] + ":00") { if(list[i] == sholatName) { alert(jsonStr.audio_before_adzan); } } } } }); On my table, I have data sholat_name | audio_before_adzan | iqamah Subuh | Alfatihah.mp3 | 10 Dhuhur | Al-Baqarah.mp3 | 10 On my PHP jSon $time10 = date("H:i:s", strtotime('+ 10 minutes')); $qAdzanIqomah = mysqli_query($con, "SELECT * FROM tb_sholat WHERE active = 'Y'"); while($dAdzanIqomah = mysqli_fetch_array($qAdzanIqomah)) { $sholatName[] = $dAdzanIqomah['sholat_name']; $iqamah[] = $dAdzanIqomah['iqamah']; $audio_before_adzan[] = $dAdzanIqomah['audio_before_adzan']; } $data = array( "audio_before_adzan" => $audio_before_adzan, 'time10' => $time10 ); echo json_encode($data); What I want is, when if(jsonStr.time10 == times[list[i].toLowerCase()] + ":00") is true then get audio_before_adzan based on sholatName. I tried the above code, but this function if(list[i] == sholatName) is not correct maybe.
I think you're just messing up your for-loops. You seem to use the same variable i for 2 different loops. You should split the loops for it to be useful: The code below is only the part in the success-function: var sholatNameLen = jsonStr.sholatName.length; for(var i=0; i<sholatNameLen; i++) { var sholatName = jsonStr.sholatName[i]; for (var j = 0; j < list.lenght; j++) { if(jsonStr.time10 == times[list[j].toLowerCase()] + ":00") { if(list[j] == sholatName) { alert(jsonStr.audio_before_adzan[i]); } } } }
Algolia Geo Search Not Working
I am having some trouble with Algolia's geo search feature which was working properly before. Here is the record of interest. I also had that indexed as described by the doc in order for me to sort it by the nearest distance: 'attributesToIndex' => ['name', 'description', 'geo'] In my client script: let settings = { aroundLatLng: '10.309813,123.893154', getRankingInfo: true, aroundRadius: 2000 }; index.search(keyword, settings, (err, data) => { console.log(data); }); But this gives me 0 hit. Notice the aroundLatLng value -- its the same value from the record of interest. Am I missing something here?
I have implemented same requirement in node.js as per given in document and its working fine. Here i am copying my whole code. Hopefully it may help you. Code /* I have used async.water model to create the setting of the index and then searching data as per given parameter. few function is custom so no need to bother about that. read each line patently. */ try { var self = this; var post = req.body; var user_id = post.user_id; var created_mode = post.user_mode == 'requester' ? 'provider' : 'requester'; var kword = post.kword; var geo = post.geo_loc; var aroundLatLng = post.aroundLatLng; var aroundRadius = !cmnFn.empty(post.radious) ? post.radious : 4500; var hitsPerPage = !cmnFn.empty(post.hitsPerPage) ? post.hitsPerPage : 20; var offset = !cmnFn.empty(post.offset) ? post.offset : 0; var length = !cmnFn.empty(post.length) ? post.length : 50; var budget_from = !cmnFn.empty(post.budget_from) ? post.budget_from : 0; var budget_to = !cmnFn.empty(post.budget_to) ? post.budget_to : 0; var day_preference = !cmnFn.empty(post.day_preference) ? post.day_preference : ''; var time_preference = !cmnFn.empty(post.time_preference) ? post.time_preference : ''; var start_date = !cmnFn.empty(post.start_date) ? post.start_date : ''; job_index is index created on Algolia var job_index = algClient.initIndex('jobs'); var cond = {}; If you are using facet & filter then you need to use filter key to execute your condition same as you may have done in sql using where clouse cond.filters = 'created_mode:"' + created_mode + '" AND (NOT user_id:"' + user_id + '")'; // Query which need to be search if (!cmnFn.empty(kword)) { cond.query = !cmnFn.empty(post.kword) ? post.kword : ''; } if ((!cmnFn.empty(budget_from) && !cmnFn.empty(budget_to)) && budget_from > 0) { cond.filters += ' AND min_charge: ' + budget_from + ' TO ' + budget_to; } if (!cmnFn.empty(day_preference)) { cond.filters += ' AND day_preference:"' + day_preference + '"'; } if (!cmnFn.empty(time_preference)) { cond.filters += ' AND time_preference:"' + time_preference + '"'; } if (!cmnFn.empty(start_date) && (new Date(start_date)).getTime() > 0) { cond.filters += ' AND start_date:"' + start_date + '"'; } Here i am setting aroundLatLng to get data nearest to far /* Do not fogot one thing, before using geo search, your records must have _geoloc key having following format "_geoloc": { "lat": 40.639751, "lng": -73.778925 } */ // Around geo search by given lat lng if (!cmnFn.empty(aroundLatLng) && !cmnFn.empty(aroundLatLng.lat)) { cond.aroundLatLng = aroundLatLng.lat + ', ' + aroundLatLng.lng; if (!cmnFn.empty(aroundRadius) && cond.aroundRadius > 0) { cond.aroundRadius = aroundRadius; } } // total number of searched record if (!cmnFn.empty(hitsPerPage)) { cond.hitsPerPage = hitsPerPage; } // Record starting position if (!cmnFn.empty(offset)) { cond.offset = offset; } // Page Limitation if (!cmnFn.empty(length)) { cond.length = length; } // Don't show attributesToHighlight in result set cond.attributesToHighlight = false; /* restrictSearchableAttributes: List of object key, where to search in given list defined in searchableAttributes */ cond.restrictSearchableAttributes = [ 'user_id', 'title', 'description', '_geoloc' ]; /* It will return raning info of result when search come under geo search Following output will return "_rankingInfo": { "nbTypos": 0, "firstMatchedWord": 0, "proximityDistance": 0, "userScore": 31, "geoDistance": 9, // Calculated distance between data geolocation given in _geoloc and search criteria in aroundLatLng "geoPrecision": 1, "nbExactWords": 0, "words": 1, "filters": 0, "matchedGeoLocation": { "lat": 28.5503, "lng": 77.2501, "distance": 9 } } */ cond.getRankingInfo = true; async.waterfall([ function (callback) { job_index.setSettings({ 'attributesForFaceting': ['user_id', 'created_mode', 'min_charge', 'day_preference', 'time_preference', 'start_date'], /* searchableAttributes: List of object key , where to search eg: ['title', 'description'] Like in sql: Where title='your searched text' AND description='your searched text' _geoloc is reserved keyword of algolia which will used to search geo location */ searchableAttributes: [ 'title', 'description', 'user_id', '_geoloc' ], /* attributesToRetrieve: Here you can specify list of key name which you want to retrive eg: ['name','address','age'] Like in sql: Select name, address, age */ attributesToRetrieve: [ '*' ] }).then(() => { return callback(null, 'success'); }); } ], function (err, results) { if (err) { console.log('error: ' + err); } job_index.search(cond).then(results => { if (results.nbHits > 0) { var rows = new Array(); for (i in results.hits) { var row = {}; var item = results.hits[i]; var user_info = {}; user_info = item.user_info; // Get distance and calculate if (!cmnFn.empty(item._rankingInfo)) { item.distance = cmnFn.meterToKM(item._rankingInfo['geoDistance']); } else { let loc = { geoLoc_1: { latitude: aroundLatLng.lat, longitude: aroundLatLng.lng }, geoLoc_2: { latitude: item._geoloc.lat, longitude: item._geoloc.lng } } cmnFn.getDistance(loc, function (distance) { item.distance = distance }) } /* self.isFav({ user_id: item.user_id, job_id: item.job_id }), function (err, flag) { item.is_favorite = flag; }; */ self.isFav({ user_id: item.user_id, job_id: item.job_id }).then(function (flag) { item.is_favorite = flag; }, function (err) { item.is_favorite = false; }); if (cmnFn.empty(item.currency)) { item.currency = "₹"; } //Delete few key from object which does not need to send in response delete item['user_info']; delete item['objectID']; delete item['_geoloc']; delete item['_rankingInfo']; row.job_info = item; row.user_info = user_info; rows.push(row); } info = { status: 1, message: util.format(lang.TOTAL_RECORD_FOUND, results.nbHits), data: rows }; cmnFn.showMsg(res, info); } else { info = { status: 0, message: lang.RECORD_NOT_FOUND, data: null }; cmnFn.showMsg(res, info); } }).catch(err => { console.log(err); info = { status: 0, message: lang.RECORD_NOT_FOUND, data: null }; cmnFn.showMsg(res, info); }); //res.end('' + JSON.stringify(results)); }); } catch (error) { info = { status: 0, message: lang.RECORD_NOT_FOUND, data: null }; cmnFn.showMsg(res, info); }
My bad. Malformed indexed data for _geoloc. Should be keyed with lat and lng
AJAX request from Codeigniter view gives an error
CSRF protection is enabled. I have a view I am trying to insert the shifts to the database table via AJAX. $('#insert_shift').click(function(e) { e.preventDefault(); var empty_td = $('.td_shift[data-type=""]').size(); if (empty_td == 0) { var date = $('#atdnc_date').val() + '-'; var arr = []; var new_arr = []; $('.td_shift').each(function() { //if($(this).attr('data-day') != 0){ var data_type = $(this).attr('data-type'); var shift_atdnc_id = $(this).attr('data-typeid'); var user_id = $(this).attr('data-user'); var new_date = date + $(this).attr('data-day'); if (data_type == 'shift') { var shift_strt_time = $(this).attr('data-start'); var shift_end_time = $(this).attr('data-end'); // Change new_arr to old var new_arr = { 'shift': shift_atdnc_id, 'user_id': user_id, 'date': new_date, 'shift_strt_time': shift_strt_time, 'shift_end_time': shift_end_time, 'checkin_time': '00:00:00', 'checkout_time': '00:00:00', 'time_spent': '00:00:00', 'checkin_reason': 'NA', 'checkout_reason': 'NA', 'work_report': 'NA', 'attn_status': 0 }; } else if (data_type == 'attendance') { var new_arr = { 'shift': shift_atdnc_id, 'user_id': user_id, 'date': new_date, 'shift_strt_time': '00:00:00', 'shift_end_time': '00:00:00', 'checkin_time': '00:00:00', 'checkout_time': '00:00:00', 'time_spent': '00:00:00', 'checkin_reason': 'NA', 'checkout_reason': 'NA', 'work_report': 'NA', 'attn_status': shift_atdnc_id }; } arr.push(new_arr); //} }); $.post(base_url + 'test_shift/insert_shift', { a: arr, csrf_test_name: csrf_token }, function(data) { alert(data); if (data == 1) { document.location.href = base_url + 'test_shift'; } else { alert("error"); } } ); } else { alert("Please fill all the shifts"); } }); If there are only 2 rows of shift, the values are getting inserted. But if there are more, 3 in this case, nothing is getting inserted to db but getting an error in the console. An Error Was Encountered The action you have requested is not allowed. When I did a search on this error I came to know that this is thrown in the case of CSRF issues. But I couldn't find a way to fix the issue. Could someone please help? UPDATE When I changed the order of data in the POST the above error has disappeared. But now another one came up. A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: mysql/mysql_driver.php Line Number: 589 And a database error. The INSERT query goes like this INSERT into table_name () values ('1','some_value'),('2','some_value2'),Array The code used to generate the array of items is same but from arr[83] the problem starts. I have tried deleting the tds after 83rd day(which is day 23 in 3rd row) from browser and the code worked. I don't understand what is happening here. The 83rd array got chopped for some reason by PHP(or CI).
The issue is fixed. Some elements(starting from array(83)) of the array that is being passed to the controller by the AJAX POST was getting popped, which lead to a database error. I don't know the exact reason yet but after some research I found this thread. So instead of inserting all values in a single INSERT query I have inserted one employee's one month's shift in one insert query, to be safe. Modified the javascript to this $('#insert_shift').click(function(e) { e.preventDefault(); var empty_td = $('.td_shift[data-type=""]').size(); if (empty_td == 0) { var num_of_days = parseInt($('.th_day').last().text()); var num_of_users = parseInt($('.user_shift_row').size()); var insert_helper = 0; var post_finish_helper = 0; var date = $('#atdnc_date').val() + '-'; var arr = []; var new_arr = []; function insert_shift_per_user() { $('.td_shift[data-inserted="0"]').each(function() { insert_helper++; var data_type = $(this).attr('data-type'); var shift_atdnc_id = $(this).attr('data-typeid'); var user_id = $(this).attr('data-user'); var new_date = date + $(this).attr('data-day'); if (data_type == 'shift') { var shift_strt_time = $(this).attr('data-start'); var shift_end_time = $(this).attr('data-end'); // Change new_arr to old var attn_status = 0; var shift = shift_atdnc_id; } else if (data_type == 'attendance') { var shift_strt_time = "00:00:00"; var shift_end_time = "00:00:00"; var attn_status = shift_atdnc_id; var shift = 0; } new_arr = { 'user_id': user_id, 'date': new_date, 'shift': shift, 'attn_status': attn_status, 'shift_strt_time': shift_strt_time, 'shift_end_time': shift_end_time }; arr.push(new_arr); if (insert_helper == num_of_days) { $.post(base_url + 'test_shift/insert_shift', { csrf_test_name: csrf_token, a: arr }, function(data) { if (data == 1) { arr = []; // clearing the array new_arr = []; // clearing the new_array insert_helper = 0; $('.td_shift[data-user="' + user_id + '"]').attr('data-inserted', 1); post_finish_helper++; if (post_finish_helper == num_of_users) { document.location.href = base_url + 'test_shift'; } else { insert_shift_per_user(); } } else { alert("Error"); } } ); } //if insert_helper == num_of_days ends }); } // FUNCTION insert_shift_per_user insert_shift_per_user(); //var aa = jQuery.parseJSON(arr); //console.log(arr); } else { alert("Please fill all the shifts"); } }); //insert_shift ends here
php to js array convert with jquery's ajax
Im trying to convert 5 PHP arrays to 5 js arrays. I used to transfer php variables to js variables with json like this: $return['variable'] = $variable; echo json_encode($return); And then fetch it as json object on the js side like this: success : function(data) { alert(data.variable); } now things are a bit more complicated, i need to transfer these 5 php arrays from a php script to my js script as 5 js arrays: PHP arrays: $i = 0; while ($location = mysql_fetch_array($get_locations)) { $location_full_name[$i] = $location['loc_full_name']; $location_main_name[$i] = $location['loc_main_name']; $location_sub_name[$i] = $location['loc_sub_name']; $location_anchor_id[$i] = $location['loc_anchor_id']; $location_type[$i] = $location['loc_type']; $i++; } and fill these corresponding arrays: var location_full_name = new Array(); var location_main_name = new Array(); var location_sub_name = new Array(); var location_anchor_id = new Array(); var location_type = new Array(); i dont know how to do this. hope i can get some help :) regards, alexander
Maybe if you post what returns in "data" so we can help you more (i think). hehe. I suggest, for your php code, where you set the data into the arrays: $i = 0; $rsl = array(); while ($location = mysql_fetch_array($get_locations)) { $rsl[$i]['full_name'] = $location['loc_full_name']; $rsl[$i]['main_name'] = $location['loc_main_name']; $rsl[$i]['sub_name'] = $location['loc_sub_name']; $rsl[$i]['anchor_id'] = $location['loc_anchor_id']; $rsl[$i]['type'] = $location['loc_type']; $i++; } echo json_encode($rsl); So to get this on the javascript // You could do the same... var location = []... var location_full_name = new Array(); var location_main_name = new Array(); var location_sub_name = new Array(); var location_anchor_id = new Array(); var location_type = new Array(); ... dataType: "json", success : function(data) { $.each(data, function(index, arr){ location_full_name[index] = arr['full_name']; ... }); }
For each of your arrays, store the value returned from json_encode. And/or make them one array/object and do the same.
You can utilize the PHP array that is actually an ordered map. Below is an example: PHP: <?php $location_full_name = array("Google, Inc.", "Siku-Siku.com"); $location_type = array("Google headquarter", "Virtual address"); $ret = array("full_name" => $location_full_name, "type" => $location_type); echo json_encode($ret); ?> JavaScript (jQuery): <script type="text/javascript"> $(function() { $.get("test.php", function(data) { console.log(data.full_name[1]); // Prints "Siku-Siku.com". }, "json"); }); </script>