Jump to content

php array for google charts api


kris1988edwards

Recommended Posts

Good morning, 

 

I am doing a small project including google charts API but I am having trouble getting  my results into the json format that is required. 

 

Google's documentation states the json format should be:

{
  "cols": [
        {"id":"","label":"Topping","pattern":"","type":"string"},
        {"id":"","label":"Slices","pattern":"","type":"number"}
      ],
  "rows": [
        {"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
        {"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
        {"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
        {"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
        {"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
      ]
}

the query works perfectly fine thats not the problem but getting it into the above format is proving hard than I expected. The below is what I am trying to do to populate the json data in the correct format. 

 

function graphdata()
{
$array['cols'][] = array('type' => 'string');
    $array['cols'][] = array('type' => 'string');
    $array['cols'][] = array('type' => 'string');


$result = sqlsrv_query($conn, $query);
while($row = sqlsrv_fetch_object($result)){ 


$array['rows'][] = array('c' => array( array('v'=>'$row->DateCreated->format('d-m-Y')'), array('v'=>$row->UnitPrice)) );


}
return $array;


}


print json_encode(graphdata());

I am using MSSQL and not the normal mysql hence the above code, i am a starter when it comes to php so I may be doing something basic totally wrong but I cannot seem to get it to work. 

 

Thanks

 

Link to comment
Share on other sites

Building your example data:

 

<?php

$data = array();
$cols = array();
array_push($cols, array("id" => "", "label" => "Topping", "type" => "string"));
array_push($cols, array("id" => "", "label" => "Slices", "type" => "number"));

$data['cols'] = $cols;

$toppings = array('mushrooms' => 3, 'Onions' => 1, 'Olives' => 1, 'Zucchini' => 1, 'Pepperoni' => 2);

$rows = array();

foreach($toppings as $topping => $quantity) {
   $elements = array();
   array_push($elements, array("v" => $topping, "f" => null));
   array_push($elements, array("v" => $quantity, "f" => null));
   $row = array("c" => $elements);
   array_push($rows, $row);
}

$data['rows'] = $rows;

echo json_encode($data, JSON_PRETTY_PRINT);

 

 

{
    "cols": [
        {
            "id": "",
            "label": "Topping",
            "type": "string"
        },
        {
            "id": "",
            "label": "Slices",
            "type": "number"
        }
    ],
    "rows": [
        {
            "c": [
                {
                    "v": "mushrooms",
                    "f": null
                },
                {
                    "v": 3,
                    "f": null
                }
            ]
        },
        {
            "c": [
                {
                    "v": "Onions",
                    "f": null
                },
                {
                    "v": 1,
                    "f": null
                }
            ]
        },
        {
            "c": [
                {
                    "v": "Olives",
                    "f": null
                },
                {
                    "v": 1,
                    "f": null
                }
            ]
        },
        {
            "c": [
                {
                    "v": "Zucchini",
                    "f": null
                },
                {
                    "v": 1,
                    "f": null
                }
            ]
        },
        {
            "c": [
                {
                    "v": "Pepperoni",
                    "f": null
                },
                {
                    "v": 2,
                    "f": null
                }
            ]
        }
    ]
}
Link to comment
Share on other sites

Does this get you any closer? (not tested)

function graphdata()
{
    $array['cols'][] = array(
            'id' => '',
            'label' => 'DateCreated',
            'pattern' => '',
            'type' => 'string'
            );
    $array['cols'][] = array(
            'id' => '',
            'label' => 'UnitPrice',
            'pattern' => '',
            'type' => 'number'
            );
            
    $result = sqlsrv_query($conn, $query);
    while($row = sqlsrv_fetch_object($result)){ 
        $array['rows'][] = array (
                        'c' =>array(
                                array ('v' => $row->DateCreated->format('d-m-Y'), 'f' => ''),
                                array ('v' => $row->UnitPrice, 'f' => ''),
                            )
                        );
    }
    return $array;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.