• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling Model From View File?

#11
[eluser]Dam1an[/eluser]
Here's something I quickly put together which should do the trick
The key is the join in the model, which joins the user and comments table together

Code:
Model
<?php
function get_comments($story_id) {
    $this->db->where('story_id', $story_id);
    $this->db->join('users', 'userd.id = comments.user_id');
    return $this->db->get('comments');
}
?>

Controller
<?php
function view_story($id) {
    $data['story'] = $this->story->get_story($id);
    $data['comments'] = $this->comment_model->get_comments($id);
    $this->load->view('story_with_comments', $data);
}
?>

View
<?php
echo $story->title;
echo $story->body;
foreach($comments as $comment) {
    echo $comment->title;
    echo $comment->user_name;
    echo $comment->body;
}
?>

Let me know if this is the sort of thing you're after, and if it works (not tested)

#12
[eluser]atlanta[/eluser]
Cool cool thanks Dam1an i was just looking at joins also i was thinking that was the way to go ..

Thanks again.

Problem Solved

#13
[eluser]Dam1an[/eluser]
Your welcome
And once you start using joins, you wonder how you ever lived without them

I came accross some of my old PHP code, and I had loads of calls just like yours was before (although mine was even worse as this was pre MVC, so all my SQL was in the views)

#14
[eluser]atlanta[/eluser]
Yea that's how mine are also in previous code lol...

#15
[eluser]Thorpe Obazee[/eluser]
@OP. Actually, Bamboo Invoice does use model calls from the view. Although, I'd prefer helpers getting the info.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.