CodeIgniter Forums

Full Version: how to access methods of controller from views
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

i am having similar problem

<title><?php echo $title; ?></title>
<h1>Welcome to my blog</h1>
<div = "posts">
&lt;?php foreach($query->result() as $row): ?&gt;
&lt;?php echo $row->title ?&gt;
&lt;?php echo $row->text ?&gt;
$comment_display = $this->comment_display($row->id);
&lt;form method = "post" action = "http://localhost/feeders/index.php/blog/comment_insert"&gt;
&lt;?php echo form_hidden('entry_id',$this->uri->segment(3)); ?&gt;
<p>&lt;textarea name="text" rows="3"&gt;&lt;/textarea></p>
<p>&lt;input type="text" name="author" /&gt;&lt;/textarea></p>
<p>&lt;input type="submit" value="Submit" /&gt;&lt;/textarea></p>
<hr />
&lt;?php endforeach; ?&gt;


My controller is
class Blog extends Controller {
function Blog()


function index()

$data['title'] = "Blog";
$data['list'] = array('first', 'second', 'third');
$data['query'] = $this->blog_model->get_blog_entries();

function comment_display($blog_id)
return $this->blog_model->show_comments($blog_id);

function comment_insert()
$this->db->insert('comments', $_POST);

my model is
Class Blog_model extends Model
function get_blog_entries()
return $this->db->get('entries');
function show_comments($row_id)
function insert_comment($data)

i am getting the error

( ! ) Fatal error: Call to undefined method CI_Loader::comment_display() in C:\wamp\www\feeders\system\application\views\blog_view.php on line 21

can any one guide me

El Forum

Hi! I think first of all, you should start with trying to understand the MVC architectural pattern.
The logic for your script should all be in your controller. This means, that you should put all the logic for fetching data from your database in the controller (eg. the foreach ($query->result_array() as $row) loop), then pass this array result into the view and then just simply echo it there or whatever you need.
Hope this helps.

El Forum

[eluser]Amitabh Roy[/eluser]
$comment_display = $this->comment_display($row->id);

You are calling a method of your controller in your view. Its wrong.
If you need to get the blogs and its associated comments you need to define the logic and pull the data in the controller itself , then arrange it and then pass it to the view. What you are doing in the view needs to be done in the controller.

index Controller code
$blogEntries = array();

$counter = 0;
foreach($query->result() as $row){
    $comment   = $this->comment_display($row->id);
    $blogEntries["$counter"]['title']   = $row->title;
    $blogEntries["$counter"]['text ']   = $row->title;
    $blogEntries["$counter"]['comment'] = $comment;

$data[‘title’] = “Blog”;
$data[‘list’] = array(‘first’, ‘second’, ‘third’);
$data[‘blogEntries’] = $blogEntries;

Also have a look at the tutorials specified in

These should help you in improving your codeigniter skills