0

This is my code:

my model : My_model

function load_modules(){

$modules = $this->db->query('SELECT name FROM module');
$result = $modules->result_array();

return $result;

}

function load_lesson($modname){

$lessons = $this->db->query(
'SELECT lesson.name 
FROM lesson 
INNER JOIN module 
ON module.id = lesson.moduleid and module.name = $modname'
); 

$result = $lessons->result_array();

return $result;

}

my controller : My_controller

$this->load->model('My_model');
$arraydata['values'] = $this->My_model->load_modules();

and in my view:

foreach($values as $modulename){

load_lesson($modulename); // This is the method from my model which
// I want to be access in my controller in a way that will work like this.

}

What I wanted to happen is to be able to get all lessons for each module. But I can't figure out of a way to do it in a controller. Hope you can help me with this. Thanks!

1
  • i'm trying to analyze your problem maybe it can be solved on optimizing your query, you selected a name from a table then used it as a reference to another table and joined by the same table? Commented Jan 30, 2013 at 13:28

1 Answer 1

1

don't try to call a method of a model from a view. try something like this in controller....

$this->load->model('My_model');
$arraydata['values'] = $this->My_model->load_modules();
foreach($arraydata['values'] as $key=>$value){
  $arraydata['values'][$key]['lessons']=$this->My_model->load_lesson($value);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that, but how can I load it to a view? How would I display the modules then the lessons for each module? Sorry, but I'm just finding it hard to figure out.
you can get all lessons in your view like this approach foreach($values as $modulename){ foreach($modulename['lessons'] as $MYlessions){ echo $MYlessions; } } try print_r($values) to view the structure of the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.