alexmngn
alexmngn

Reputation: 9627

Where formatting data ? Model ? Controller?

I need to format data to use them after MySQL request.

Format date (using date library), sort and arrange data (using loop) etc.

Php example (CodeIgniter framework):

$result = $query->result_array();
$data = array();
foreach($result as $key => $value) {
    $result[$key]['date'] = px_date_time_format($result[$key]['date'], 'fr');       
    if ($value['id_parent'] == 0) {
        $data[$value['id']] = $result[$key];
    }
}

foreach($result as $key => $value) {
    if ($value['id_parent'] != 0) {
        $data[$value['id_parent']]['children'][$value['id']] = $value;  
    }
}

return $data;

Where should I format this data? In my Model or my Controller?

Upvotes: 0

Views: 722

Answers (1)

tereško
tereško

Reputation: 58444

In the view.

View in MVC design pattern is the part which is responsible for presentation logic. It create the response, that browser (assuming you are talking about adaptation of MVC for web) receives.

View requests data from model layer and, based on data, that it receives, and state previously set by controller, decides what response should it produce. I can be either HTML (composed from multiple templates) JSON, XML or even just a HTTP header.

Controller deals with user's input and model layer contains the business logic.

Upvotes: 2

Related Questions