I have a project that uses Angular and Codeigniter. I have a graph which uses an array as input. I use the following code to build the array in Codeigniter controller:
function get_graph_data()
{
$project_id = $this->site_settings->get_decrypt_id($this->input->post('project_id'));
$year = ($this->input->post('year') ? $this->input->post('year') : date('Y'));
$result = $this->Income_expense_model->get_graph_data($project_id, $year);
for ($i = 1; $i <= 12; $i++) {
$dummy_date = strtotime("1991-" . $i . "-13");
$month_name = date('F', $dummy_date);
$array['months'][$i] = $month_name;
$array['income'][$i] = "0";
$array['expense'][$i] = "0";
}
foreach($result as $row) {
if ($row->type == 1) $array['income'][$row->month_num] = $row->amount;
else $array['expense'][$row->month_num] = $row->amount;
}
$data['months'] = array_values($array['months']);
$data['income'] = array_values($array['income']);
$data['expense'] = array_values($array['expense']);
echo json_encode($data);
}
I use a service to get this array from my Angular controller and uses it there.
Am I doing the right thing? Where should I build the array?
1. In the Codeigniter controller like I did
or
2. By getting just the
$result = $this->Income_expense_model->get_graph_data($project_id, $year);
array which is not formatted from the Codeigniter controller and building it in the Angular controller?
or something else?