Turn database output to JSON in Amcharts default

Asked

Viewed 61 times

1

I’m using the Code Igniter framework. I have the following model:

public function getVendasVendedor() {
        $query = $this->db->query("SELECT * FROM vendas_vendedor");

        return $query->result();    
}

My Controller:

public function retornaJsonVendasVendedor() {
    echo json_encode($this->vendasm->getVendasVendedor());
}

Returning JSON from the controller:

[{
    "anomes":"201601",
    "totven":"1218000.0000",
    "codven":"117285",
    "nomeven":"JOAO FELIPE RIBAS"
},
{
    "anomes":"201701",
    "totven":"78000.0000",
    "codven":"117285",
    "nomeven":"JOAO FELIPE RIBAS"
},
{
    "anomes":"201602",
    "totven":"2800.0000",
    "codven":"109959",
    "nomeven":"JIAN LEANDRO FERMINO"
}]

Debug the Model output:

Array
(
[0] => stdClass Object
    (
        [anomes] => 201601
        [totven] => 1218000.0000
        [codven] => 117285
        [nomeven] => JOAO FELIPE RIBAS
    )

[1] => stdClass Object
    (
        [anomes] => 201701
        [totven] => 78000.0000
        [codven] => 117285
        [nomeven] => JOAO FELIPE RIBAS
    )

[2] => stdClass Object
    (
        [anomes] => 201602
        [totven] => 2800.0000
        [codven] => 109959
        [nomeven] => JIAN LEANDRO FERMINO
    )
)

and I need to generate a JSON, in the following pattern, which would be for a chart using amcharts:

[{
  "category": "2013-08-24",
  "value1": 417,
  "value2": 127
}, {
  "category": "2013-08-25",
  "value1": 417,
  "value2": 356
}]

My question is how to distribute the bank information to generate JSON. To have the result more or less the way below:

resultado

  • $json = json_encode($myObject); ? Your question is not very clear. You have to manipulate the value of the keys as well?

  • @Caiofelipepereira, the idea would be to put in Category for example 201601, and in the values, the sellers who sold, with the value of each one. Because from what I saw, every "value" would be a column on the chart

No answers

Browser other questions tagged

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