views:

91

answers:

2

Dear friends,

Can anyone please explain me the object flow in codeigniter MVC ? I can see for example when I put the followong code in controller it works, but i am not being able to figure out which part of this goes in model in vews. I tried several ways but coudn't. When i use the example codes from other it works but myself i am getting confused. Please help

    $query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}
+1  A: 

That would translate into something like:

Model:

class SomeModel extends Model {
    function SomeModel() {
        parent::Model();
    }

    function get_some_data() {
        return $this->db->query('some_query')->result_array();
    }
}

Controller:

class SomeController extends Controller {
    function SomeController() {
        parent::Controller();
    }

    function index() {
        $this->load->model('SomeModel');
        $some_data = $this->SomeModel->get_some_data();
        $this->load->view('some_view');
    }
}

View:

foreach($some_data as $data) {
    echo $data->title;
    echo $data->name;
    echo $data->body;
}

However, for your communication between controller and view I would recommend using a template parser such as Dwoo or Twig (I don't like the one that comes with CI).

Daniel
is it necessary to append the .php to the view ? E.g. `$this->load->view('some_view.php');`
Russell Dias
@RussellDias: nope, you're right it isn't :) edited!
Daniel
A: 

On the controller part I was doing:

class SomeController extends Controller {
    function SomeController() {
        parent::Controller();
    }

    function index() {
    }

    function show_data(){
            $this->load->model('SomeModel');
            $some_data = $this->SomeModel->get_some_data();
           $this->load->view('some_view.php');
           $this->index()
    }
}

is that not the way to do it when we have many function ? When i look at other's code I see something like that or I am wrong ?

sagarmatha
@Pokhara: That depends on the structure of your application. I do however recommend that you only use public functions for your actual pages, and make your internal functions private. I'm not sure what you're trying to do there though.
Daniel