How to mount the return Json

Asked

Viewed 239 times

0

I don’t know how to do it or who to turn to. I need to make this json return with Jquery.Ajax

I’d like to pass parameters more or less like this

In JAVASCRIPT I have so

        $.ajax({
            type: "POST",
            url: "../execs/proc_fluxocaixa.php",
            data: {d_acao: "grafico", c_tipo: tipo, c_dia: dia},
            dataType: "json",
            success: function(ret_dados){
               var pageviews ret_dados["r_dados"];
            }
        });

My PHP has so:

if($acao == "grafico"){
    $tipo = $_POST["tipo"];
    $dias = 30;
    $mes  = 3;

    if ($tipo == 0){
        ///Debito

        $Saida[] = array();
        for($i=1; $i <= $dias; $i++) {
            $dataC = date("Y-$mes-$i");

            $diaSQL = $Sv->selectDB("SELECT SUM(valor) TotalDia FROM mov_contas WHERE tipo = 'D' AND apagado = 0 AND banco = 0 AND data >= '$dataC 00:00:00' AND data <= '$dataC 23:59:59' GROUP BY data");

            if (count($diaSQL) == 0){
                $TotalValDia = 0;
            } else {
                $TotalValDia = $diaSQL[0]["TotalDia"];
            }

            $Saida[] = $i . "," . $TotalValDia;
        }
    }

    $retorno["r_dados"] = $Saida;
}

How I do in PHP and return to look like this:

            var pageviews = [
                [1, randValue()],
                [2, randValue()],
                [3, 2 + randValue()],
                [4, 3 + randValue()],
                [5, 5 + randValue()],
                [6, 10 + randValue()],
                [7, 15 + randValue()],
                [8, 20 + randValue()],
                [9, 25 + randValue()],
                [10, 30 + randValue()],
                [11, 35 + randValue()],
                [12, 25 + randValue()],
                [13, 15 + randValue()],
                [14, 20 + randValue()],
                [15, 45 + randValue()],
                [16, 50 + randValue()],
                [17, 65 + randValue()],
                [18, 70 + randValue()],
                [19, 85 + randValue()],
                [20, 80 + randValue()],
                [21, 75 + randValue()],
                [22, 80 + randValue()],
                [23, 75 + randValue()],
                [24, 70 + randValue()],
                [25, 65 + randValue()],
                [26, 75 + randValue()],
                [27, 80 + randValue()],
                [28, 85 + randValue()],
                [29, 90 + randValue()],
                [30, 95 + randValue()]
            ];
  • show how vc returns the array in php, so it’s easier to help

  • I did not parse much your php code, but you have to change the data from ajax by something like this: {"d_acao": "grafico", "c_tipo": tipo, "c_dia": dia}, Place a console.log(ret_dados) after the success and see what returns

  • It is returning [Array[0], "1,1850.00", "2,0", "3,0", "4,0", "5,0", "6,0", "7,0", "8,0", "9,0", "10,0", "11,0", "12,0", "13,0", "14,0", "15,0", "16,0", "17,0", "18,0", "19,0", "20,0", "21,0", "22,0", "23,0", "24,0", "25,0", "26,0", "26,0", ", "28,0", ", "29,0", "30,0", ""]

  • In fact as it would be correct to collect this data, I want to pick up the total of the day at the cashier drive to play on the chart, where type = 0 means it is debt.

  • agr vc just need to format the array according to the graphics plugin q vc ta using

  • var Plot = $.Plot($("#chart_2"), [{ date: mov_debito, label: "Sa&iacute;das", Lines: { lineWidth: 1, }, shadowSize: 0 } needs to look like this&#xTo; var mov_debito = [ [1, randValue()], [2, randValue()], [3, 2 + randValue()], [4, 3 + randValue()] ];

  • Just look: var mov_debito = ""; $.ajax({&#xA; type: "POST",&#xA; url: "../execs/proc_fluxocaixa.php",&#xA; data: {d_acao: "grafico", c_tipo: 0},&#xA; dataType: "json",&#xA; success: function(ret_dados){&#xA; mov_debito = ret_dados["r_dados"]; console.log(mov_debito); } }) Alert(mov_debito);

Show 2 more comments
No answers

Browser other questions tagged

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