Reputation: 805
I have in my site notifications, similar as facebook. But I only have 3 type of notifications:
Table notification
notification_id | type | item_id | from_id | to_id | created | notified
item_id represents quize_id or group_id. I make extra joins to get all data.
A problem I have is how to display each notification depends on type. One of options is to organize string for display in controller:
foreach($results as $key => $result){
switch($result['type']){
case 1:
$string = "<a href=".baseUrl()."/users/$result['userId']>$result['nickname']</a> "._("wants you to take a quize")." <a href='".baseUrl()."/quize/$result['rel']'>$result['quizeName']</a>";
break;
case 2:
$string = "<a href=".baseUrl()."/users/$result['userId']>$result['nickname']</a> "._("added you to his group")." <a href='".baseUrl()."/group/$result['rel']'>$result['groupName']</a>";
break;
case 3:
$string = _("You received 100 credit for log in today");
}
$notifications[$key]['string'] = $string;
}
$data['notifications'] = $notifications;
$this->load->view('views/notifications/last_notification_view', $data);
The problem with that method is that part of view is inside controller. I could sent rough data to view, but then the problem is that I need to make similar php code in view. So which method is better? Or is there any other way?
Upvotes: 3
Views: 415
Reputation: 58444
The best option would create a real view instead of using templates.
This code is clearly part of presentation logic, which is in purview of views in MVC design pattern. Unfortunately for you CodeIgniter is using Rails-like architecture, which means that you will have to use helpers, thus pushing you even farther away from MVC-inspired design patterns.
Upvotes: 2
Reputation: 5607
My own personal opinion is that you should pass $results to the view, and have the loop that takes results and prints out links inside of view. What you should have is this in controller:
$data['notifications']=$results;
$this->load->view('views/notifications/last_notification_view', $data);
and this in view:
foreach($data['notifications'] as $key => $result){
switch($result['type']){
case 1:
echo "<a href=".baseUrl()."/users/$result['userId']>$result['nickname']</a> "._("wants you to take a quize")." <a href='".baseUrl()."/quize/$result['rel']'>$result['quizeName']</a>";
break;
case 2:
echo "<a href=".baseUrl()."/users/$result['userId']>$result['nickname']</a> "._("added you to his group")." <a href='".baseUrl()."/group/$result['rel']'>$result['groupName']</a>";
break;
case 3:
echo _("You received 100 credit for log in today");
}
}
Upvotes: 4