how to display content from database in codeigniter without calling controller from view?

codeigniter, mysql, php

Solution

I think the CodeIgniter documentation is actually very clear, and very easy to follow. Nevertheless, here's an example of how you might structure your app.

Controller:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Members extends CI_Controller {
    function __construct() {
        parent::__construct();

        $this->load->model('members_model');
    }

    function index() {
        $data['members'] = $this->members_model->get_members();

        $this->load->view('header', $data);
        $this->load->view('members');
        $this->load->view('footer');
    }
}

Model:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Members_model extends CI_Model {

    function get_members() {
        $query = $this->db->get('members');
        return $query->result_array();
    }
}

View:

<?php

print_r($members);

To access the above page, you would visit http://yoursite.com/index.php/members.

You can use a `.htaccess` file to remove the `index.php` segment from the URI. The `index()` method is automatically called when you run a controller.

Problem

I am new to codeigniter. I followed a video tutorial and successfully created a login registration system. After registration or login the users reach a page called members.php. members.php is a view. my controller is main.php and model is model-users.php. I want members.php to show/have content from the database so that the users can read it once the login or register? I followed a few resources on the web that said not to call controller from view. How can I accomplice the above without doing so? Thanks

Original source