How do I make a multidimensional array return by ajax

Asked

Viewed 55 times

0

good morning, I have the following code (this working), it returns me to Ajax an array with integers.

<?php 

date_default_timezone_set('America/Sao_Paulo');

require_once '../conexao/conexao.php';
require_once 'config.php';


 $CentroCusto = $_POST["selectDepartamento"];
 $CodigoLocal = $_POST["selectLocal"];
 //lista de arrays
 $diasCorridos = array('01','02','03','04','05','06');
 $metaRealizada = array(500,1000,1500,2000,2500,2000);
 $vendasRealizada = array(300,700,1200,1000,1500,1300);
 $decontosAplicados = array(100,200,400,300,500,400);

 //VARIAVEIS QUE PRECISA DEVOLVER PRO AJAX
//  $dadosGrafico = array(
//     'diasCorridos' => $diascorridos,
//     'metaRealizada' => $metaRealizada,
//     'vendasRealizada' => $vendasRealizada,
//     'decontosAplicados' => $decontosAplicados,

//  );
 echo json_encode($diasCorridos);
?>

and in ajax I get like this

     pageurl = '././helpers/SQLResumoMesLinear.php';

  $.ajax({
    url: pageurl,
    type: 'POST',
    cache: false,
    dataType: 'json',
    data: dadosajax,
    error: function (result) {
      alert("aconteceu algum erro ao carregar o grafico" + result);
    },
    success: function (result) {

      var diasCorridos = [];
      for(var i in result){
        diasCorridos.push(result[i]);
      }

However I need to pass Ajax more than one array, because for the assembly of a graph I need several array, my doubt as I send several array in a return and as I take these array in Ajax and pass to the variables.

I’ll plot by inserting the array

 var ctx = document.getElementById("resumoMesAtual");
      var myLineChart = new Chart(ctx, {
        type: 'line',
        data: {
          labels: diasCorridos, //aqui esta o unico array que eu retornei e funciona
          datasets: [{
            label: "Meta",
            lineTension: 0.4,
            backgroundColor: "rgba(229, 240, 20, 0.00)",
            borderColor: "rgba(229, 240, 20, 0.50)",
            pointRadius: 2,
            pointBackgroundColor: "rgba(229, 240, 20, 0.50)",
            pointBorderColor: "rgba(229, 240, 20, 1)",
            pointHoverRadius: 1,
            pointHoverBackgroundColor: "rgba(229, 240, 20, 1)",
            pointHoverBorderColor: "rgba(229, 240, 20, 1)",
            pointHitRadius: 1,
            pointBorderWidth: 0.1,
            data: //aqui vai outro array
          },
          {
            label: "Realizado",
            lineTension: 0.3,
            backgroundColor: "rgba(20, 240, 64, 0.07)",
            borderColor: "rgba(20, 240, 64, 1)",
            pointRadius: 3,
            pointBackgroundColor: "rgba(20, 240, 64, 1)",
            pointBorderColor: "rgba(20, 240, 64, 1)",
            pointHoverRadius: 3,
            pointHoverBackgroundColor: "rgba(20, 240, 64, 1)",
            pointHoverBorderColor: "rgba(20, 240, 64, 1)",
            pointHitRadius: 10,
            pointBorderWidth: 0.1,
            data://aqui vai outro array
          },
          {
            label: "Descontos",
            lineTension: 0.3,
            backgroundColor: "rgba(240, 20, 20, 0.50)",
            borderColor: "rgba(240, 20, 20, 1)",
            pointRadius: 3,
            pointBackgroundColor: "rgba(240, 20, 20, 1)",
            pointBorderColor: "rgba(240, 20, 20, 1)",
            pointHoverRadius: 3,
            pointHoverBackgroundColor: "rgba(240, 20, 20, 1)",
            pointHoverBorderColor: "rgba(240, 20, 20, 1)",
            pointHitRadius: 10,
            pointBorderWidth: 0.1,
            data: //aqui vai outro array

Thanks in advance

1 answer

1


I managed to solve by sending a PHP matrix to ajax like this:

 //lista de arrays
 $diasCorridos = array('01','02','03','04','05','06');
 $metaRealizada = array(500,1000,1500,2000,2500,2000);
 $vendasRealizada = array(300,700,1200,1000,1500,1300);
 $decontosAplicados = array(100,200,400,300,500,400);

 //VARIAVEIS QUE PRECISA DEVOLVER PRO AJAX
 $dadosGrafico = array(
    $diasCorridos,
    $metaRealizada,
    $vendasRealizada,
    $decontosAplicados
 );
 echo json_encode($dadosGrafico);

And in ajax I received so:

success: function (result) {

  var resultado = [];
  for(var i in result){
    resultado.push(result[i]);
    // console.log('variavel I'+i);
    // console.log(result[i]);
  }

  console.log(resultado[0]); //mostra os dias corridos
  console.log(resultado[1]); //mostra a meta realizada 

then I just put it on the chart like this :

 labels: resultado[0],
          datasets: [{
            label: "Meta",
            lineTension: 0.4,
            backgroundColor: "rgba(229, 240, 20, 0.00)",
            borderColor: "rgba(229, 240, 20, 0.50)",
            pointRadius: 2,
            pointBackgroundColor: "rgba(229, 240, 20, 0.50)",
            pointBorderColor: "rgba(229, 240, 20, 1)",
            pointHoverRadius: 1,
            pointHoverBackgroundColor: "rgba(229, 240, 20, 1)",
            pointHoverBorderColor: "rgba(229, 240, 20, 1)",
            pointHitRadius: 1,
            pointBorderWidth: 0.1,
            data: resultado[1],
          },
          {
            label: "Realizado",
            lineTension: 0.3,
            backgroundColor: "rgba(20, 240, 64, 0.07)",
            borderColor: "rgba(20, 240, 64, 1)",
            pointRadius: 3,
            pointBackgroundColor: "rgba(20, 240, 64, 1)",
            pointBorderColor: "rgba(20, 240, 64, 1)",
            pointHoverRadius: 3,
            pointHoverBackgroundColor: "rgba(20, 240, 64, 1)",
            pointHoverBorderColor: "rgba(20, 240, 64, 1)",
            pointHitRadius: 10,
            pointBorderWidth: 0.1,
            data:resultado[2] ,
          },
          {
            label: "Descontos",
            lineTension: 0.3,
            backgroundColor: "rgba(240, 20, 20, 0.50)",
            borderColor: "rgba(240, 20, 20, 1)",
            pointRadius: 3,
            pointBackgroundColor: "rgba(240, 20, 20, 1)",
            pointBorderColor: "rgba(240, 20, 20, 1)",
            pointHoverRadius: 3,
            pointHoverBackgroundColor: "rgba(240, 20, 20, 1)",
            pointHoverBorderColor: "rgba(240, 20, 20, 1)",
            pointHitRadius: 10,
            pointBorderWidth: 0.1,
            data: resultado[3],
          }],

Browser other questions tagged

You are not signed in. Login or sign up in order to post.