在不破坏MVC框架的情况下在CodeIgniter中加载模型

I'm really a newbie in MVC Framework and as well as in CodeIgniter. Is it possible to access model in view in CodeIgniter without breaking MVC framework? This is what I'm talking about.

Below is my code for my view.

foreach ($packages as $row)
{
    if($row->Category == "Wedding")
    {
        $package_name = $row->package_name;
        $ratings = $model->ratings($package_name);
        foreach ($ratings as $row_review)
        {

        }
    }
}

And this is in my model

public function ratings($package_name)
{
    $this->db->select('round(sum(Rating)/count(Rating)) as total');
    $this->db->where('package_name', $package_name);
    $query = $this->db->get('tbl_review');
    return $query->result();
}

Hope this will help you :

Note : your database and model should also be loaded either in controller or in autoload.php;

You can do it with the help of condeigniter's helpers . Create a file and name it custom_helper.php and add it to helpers folder and load it with autoload.php like this :

$autoload['helper'] = array('custom');

In your custom_helper.php add a function like this :

function package_ratings($package_name)
{ 
    $ci = & get_instance();
    $ci->db->select('round(sum(Rating)/count(Rating)) as total'); 
    $ci->db->where('package_name',$package_name);
    $query = $ci->db->get('tbl_review'); 
    return $query->result(); 
}

In your view use package_ratings function as the given below :

foreach($packages as $row)
{ 
    if($row->Category == "Wedding")
    { 
        $package_name = $row->package_name; 
        $ratings = package_ratings($package_name); 
        foreach ($ratings as $row_review) 
        { 

        }
    }
}

For more : https://www.codeigniter.com/user_guide/general/helpers.html

In View can call the model function directly:

$CI =& get_instance();
$CI->load->model('your_model');// If the model was not loaded before(in controller or via autoloader).
....
$result = $CI->your_model->model_method();