When querying the database in my model, I use the following function:
function graphRate($userid, $courseid){
$query = $this->db->get('tblGraph');
return $query->result();
}
The data retrieved by my model is then encoded in JSON format in my controller like this:
if($query = $this->rate_model->graphRate($userid, $courseid)){
$data['graph_json'] = json_encode($query);
}
$this->load->view('graph', $data);
As a result, I receive a JSON object structured like this:
[
{"id":"1","title":"myTitle","score":"16","date":"2013-08-02"},
{"id":"2","title":"myTitle2","score":"17","date":"2013-09-02"},
{"id":"3","title":"myTitle3","score":"18","date":"2013-10-02"}
]
Within my graph view, I include an external JavaScript file like this:
<script type="text/javascript" src="script.js"></script>
Now, I need to pass the $data from my controller to my external script.js in order to use it as labels and data for my chart. How can I achieve this?
One more thing regarding the JSON data, is there a way to format the output as follows:
{
"obj1":{"id":"1","title":"myTitle","score":"16","date":"2013-08-02"},
"obj2":{"id":"2","title":"myTitle2","score":"17","date":"2013-09-02"},
"obj3":{"id":"3","title":"myTitle3","score":"18","date":"2013-10-02"}
}