简体   繁体   中英

Codeigniter loading two different data array from a database with the second one is dependent with the first one and load into a view

This is my code:

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 :

$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!

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM