codeigniter从视图中填充下拉菜单 - 从视图中触发方法

Hello I am a beginner in codeigniter framework and I need some help. I have a form (in the table organisation) that needs to be populated with all sorts of data. One field (first row, second column) need to be populated with drop down meny (with data from table from database) (select option menu), and I manage to to that,

form table

But this is what I have done to do that:

when user click on link (T - Insert Labwork)

link

actually he calls controller's method (my_courses()) and that method :

1 - takes username from session

2 - loads model.

3 - then he pass username to the loaded model

4 - if model returns false than we will returns message to the user

5 - else if model returns true then we will return results

CONTROLLERS method looks like this:

function my_courses(){

$username = $this->session->userdata('username'); //1 username from session 

$this->load->model('model_professor'); //2 we load a model,whose task is to load professors courses from table

$result =$this->model_professor->professors_classes($username); //3 so we send username to that model 

if($result == false){ 
    echo "Professor has no courses ";
}else{ //4 model will return results collected from table 
    $query = $this->model_professor->professors_classes($username);
    $data['main_content'] = 'professor/t_professor_insert_labwork_2';
    $data['another_content'] = 'professor/professor_view_links';
    $data['records'] = $query;
    $this->load->view('includes/template',$data);
    }       
}

MODEL method looks like this:

function professors_classes($username){

    $this->db->where("username", $username); 

    $query = $this->db->get("table_teach_a_course");  

    if($query->result()){
        return $query->result();
    }else{
        return false;
    }           
}

And finally my VIEW (shorter version,without all rows from the image) t_professor_insert_labwork_2 looks like this:

 <?php
    echo form_open("controller_professor/insert_labwork");
?>
<table>
    <tr>
        <td>Course name:</td>
        <td>
            <?php if(isset($records)) { ?>
                        <select>
                            <?php foreach ($records as $row){ ?>
                                <option value=""> <?php echo $row->course_code; ?> </option>
                            <?php } ?>
                        </select>
                     <?php
                    }else { 
                        echo"<h6>No records were returned!</h6>";
                    } ?> 
        </td>
    </tr>


    <tr> <td colspan="2"> <hr/> </td></tr>

    <tr>
        <td><input type="reset" value="Clear form"></td>
        <td></td>
        <td><input type="submit" value="Save"></td>
    </tr>
</table>
<?php
    echo form_close();
?>
    <?php
        echo validation_errors('<p style="color:red"></p>');
    ?>

But when I click on link for example (home page) enter image description here

he also loads that view and some others as well, and when he loads my t_professor_insert_labwork_2 view the problems occurs.

This is what HOME PAGE link calls:

$this->load->view('professor/t_professor_insert_labwork_2');

But that just loads the view who is empty and does not have populated drop down menu, and I understand why, because there is nothing to trigger my method to populate that menu. But this is what cause trouble to me, I just don't know how to trigger from my view some method that will return data to that field.

And my question is, how can I call my view, that will automatically trigger some methhod who will return data to fill this drop down field?

Thanks to everyone willing to help me!!!!

You need to modify your controllers DEFAULT method (the one thats called when you go to www.mysite.com/mycontroller) this would generally be named after your controller or called index, I am going to give the example using the code you provided assuming that this method is your default method. Looking at this code there are two possible cases.

CASE 1 - You do not have a username session variable on initial load.

function my_courses(){
  
$this->load->model('model_professor'); //2 we load a model,whose task is to load professors courses from table

//set a default for username
 $username = 'mydefaultusername';
//check if a username has been provided and update $username variable
if(isset($this->session->userdata('username')) && !empty($this->session->userdata('username'))){
  $username = $this->session->userdata('username'); //1 username from session 
}

$result =$this->model_professor->professors_classes($username); //3 so we send username to that model 
  
if($result == false){ 
    echo "Professor has no courses ";
}else{ //4 model will return results collected from table 
    $query = $this->model_professor->professors_classes($username);
    $data['main_content'] = 'professor/t_professor_insert_labwork_2';
    $data['another_content'] = 'professor/professor_view_links';
    $data['records'] = $query;
    $this->load->view('includes/template',$data);
    }       
}

CASE 2 - You have a username session variable but want a default result set if there are none.

function my_courses(){

$username = $this->session->userdata('username'); //1 username from session 

$this->load->model('model_professor'); //2 we load a model,whose task is to load professors courses from table

$result = $this->model_professor->professors_classes($username); //3 so we send username to that model 

//check if you need to set a default
if(count($result) !> 0){
  $result = //your method to get your default results.  
}
if($result == false){ 
    echo "Professor has no courses ";
}else{ //4 model will return results collected from table 
    $query = $this->model_professor->professors_classes($username);
    $data['main_content'] = 'professor/t_professor_insert_labwork_2';
    $data['another_content'] = 'professor/professor_view_links';
    $data['records'] = $query;
    $this->load->view('includes/template',$data);
    }       
}

In the second case you would have to set a session or other variable upon the initial load of the controller to make sure that you dont return your default results when you should return "none found" i.e.

    //check if you need to set a default
    if(count($result) !> 0 && !isset($_SESSION['init']) or $_SESSION['init'] == FALSE){
      $_SESSION['init'] = 1;
      $result = //your method to get your default results.  
    }
</div>