Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share|improve this question

It depends.

Your question is: Where should the data be formatted, server side(Codeigniter) or client side (Angular)?

For server side, sometimes you may get too much information from your model, you may not want to expose too much information from this API (eg: user's information), so you may remove them at server side before response.

For client side, sometimes the server side's response data structure may not fit what you want, so you can re-format the data in client side.

share|improve this answer

I would suggest to use server side.

Create a graph data array on server side in such a format that can be easy for graph plugin. I am just assuming that you are not using multiple graph data for comparison. In that case you would want to use both, server side and client side.

Why server side?

All JavaScript processing happens on client's browser which also consume resources from client's machine. That means, if my browser or machine has low resources(RAM, graphics rendering engine etc), then you will see a lag in animation while graph is getting rendered. Server has more and high end resources than a browser can have. It's always better to utilize those resources if that possible.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.