Welcome Guest, Not a member yet? Register   Sign In
how to access methods of controller from views
#1

[eluser]adityajoshi[/eluser]
i am having similar problem

<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>Welcome to my blog</h1>
<ol>
<div = "posts">
&lt;?php foreach($query->result() as $row): ?&gt;
&lt;?php echo $row->title ?&gt;
&lt;?php echo $row->text ?&gt;
</div>
&lt;?php
$comment_display = $this->comment_display($row->id);
?&gt;
&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>
&lt;/form&gt;
<hr />
&lt;?php endforeach; ?&gt;

</ol>
&lt;/body&gt;
&lt;/html&gt;



My controller is
&lt;?php
var_dump($_POST);
class Blog extends Controller {
function Blog()
{
parent::Controller();
$this->load->scaffolding('comments');
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('blog_model');

}

function index()
{

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

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

function comment_insert()
{
$this->db->insert('comments', $_POST);
redirect('blog');
}
}
?&gt;

my model is
&lt;?php
Class Blog_model extends Model
{
function get_blog_entries()
{
return $this->db->get('entries');
}
function show_comments($row_id)
{
$this->db->where('row_id',$row_id)->get('comments');
}
function insert_comment($data)
{
$this->db->insert('comments',$data);
}
}
?&gt;

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
#2

[eluser]Benito[/eluser]
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.
#3

[eluser]Amitabh Roy[/eluser]
Code:
$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
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;
    
    $counter++;
   }

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


Also have a look at the tutorials specified in http://codeigniter.com/wiki/Category:Help::Tutorials

These should help you in improving your codeigniter skills




Theme © iAndrew 2016 - Forum software by © MyBB