Search code examples
phpcodeignitermodel-view-controllerform-fields

CodeIgniter3 form_dropdown() error: Undefined variable


I want to populate a list of names in a dropdown list using form_dropdown(...) and fetch the matching name accordingly.

Both my model and controller classes seem to be working fine, but I am unable to pass in the second argument to the form_dropdown() function in my view. I get the error "Undefined variable: branch", I tried dumping the values returned by getBranch() and it works. I have 3 different PHP scripts for the views: 'index' (showing the data in form tables etc), 'form'(where I fill in the details for a new student) and 'index-datatable' (Jquery script for the datatable). Please note, all the views are in one directory/folder 'students' as seen in the controller's index function. I am new to Codeigniter 3.

Controller Class

public function __construct() {
    parent::__construct();
    is_login();
    $this->load->library('form_validation');
    $this->load->helper(array('form', 'url'));
    $this->load->database();
    $this->load->model('menu_model', 'menu');
    $this->load->model('my_model', 'my');
    $this->load->model('students_model', 'students');

}

public function index() {
    $data['branch'] = $this->students->getBranch(); //Get the names of branches. 
    $data['title'] = 'Student List Home';
    $data['page'] = 'students/index'; 
    $data['datatable'] = 'students/index-datatable';
    $this->load->view('back/layouts/main', $data);
}

Model Class

public function getBranch()
{
    $this->db->select('id');
    $this->db->select('name');
    $this->db->from('branch');        
    
    $query = $this->db->get();
    $result = $query->result();

    $branch_id = array('-CHOOSE-');
    $branch_name = array('-CHOOSE-');

    for ($i = 0; $i < count($result); $i++) {
        array_push($branch_id, $result[$i]->id);
        array_push($branch_name, $result[$i]->name);
    }
    return $branch_result = array_combine($branch_id, $branch_name);   
}

View (Form, not index)

<div class="form-group">
    <label for="branch">Branch</label>
    <?php
    $attributes = 'class = "form-control" id = "branch"';
    echo form_dropdown('branch', $branch, set_value('branch'), $attributes);
    ?>
</div>

Add method

public public function add()
{
    if (!$_POST) {
        $input = (object) $this->students->getDefaultValues();
    } else {
        $input = (object) $this->input->post(null, true);
    }

    $this->form_validation->set_rules('package_name', 'Title', 'required', [
        'required' => 'Text can not be empty'
            ]
    );
    $this->form_validation->set_rules('package_fee', 'Text', 'required', [
        'required' => 'Text can not be empty'
            ]
    );
    $this->form_validation->set_rules('duration', 'Text', 'required', [
        'required' => 'Text can not be empty'
            ]
    );
    $this->form_validation->set_rules('category_id', 'Text', 'required', [
        'required' => 'Text can not be empty'
            ]
    );

    if ($this->form_validation->run() == false) {
        $data['title'] = 'Register a new Student';
        $data['page'] = 'students/form';
        $data['form_action'] = base_url("students/add");
        $data['input'] = $input;
        $this->load->view('back/layouts/main', $data);
    } else {
        $data = [
            'title' => $this->input->post('title', true),
            'firstname' => $this->input->post('firstname', true),
            'lastname' => $this->input->post('lastname', true),
            'othername' => $this->input->post('othername', true),
            'gender' => $this->input->post('gender', true),
            'dob' => $this->input->post('dob', true),
            'nationality' => $this->input->post('nationality', true),
            'address' => $this->input->post('address', true),
            'email' => $this->input->post('email', true),
            'phone_number' => $this->input->post('phone_number', true),
            'education' => $this->input->post('education', true),
            'occupation' => $this->input->post('occupation', true),
            'nationalidtype' => $this->input->post('nationalidtype', true),
            'id_number' => $this->input->post('id_number', true),
            'prev_driving_exp' => $this->input->post('prev_driving_exp', true),
            'photo' => $this->input->post('photo', true),
            'license_type_id' => $this->input->post('license_type_id', true),
            'branch_id' => $this->input->post('branch_id', true),
            'group_id' => $this->input->post('group_id', true)
        ];


        if (!empty($_FILES['photo']['name'])) {
            $upload = $this->students->uploadImage();
            $data['photo'] = $upload;
        }

        $this->students->insert($data);
        $this->session->set_flashdata('success', 'Student Data added successfully');

        redirect(base_url('students'));
    }
}

Main View only showing where the other pages is loaded. Some scripts taken off, that is the header and footer scripts.

<div id="wrapper">
    <?php $this->load->view('back/layouts/_sidebar') ?>
    <div id="content-wrapper" class="d-flex flex-column">
        <div id="content">
            <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
                <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                    <i class="fa fa-bars"></i>
                </button>
                <?php $this->load->view('back/layouts/_topbar') ?>
            </nav>
            <?php $this->load->view('back/pages/' . $page) ?>
        </div>
        <?php $this->load->view('back/layouts/_footer') ?>
    </div>
</div>

Solution

  • You're not passing a branch variable to the form template in the add function.

    Add the branch in the if, like this:

    public function add() {
    
        // ...
    
        if ($this->form_validation->run() == false) {
            $data['branch'] = $this->students->getBranch(); //Get the names of branches. 
            $data['title'] = 'Register a new Student';
            $data['page'] = 'students/form';
            $data['form_action'] = base_url("students/add");
            $data['input'] = $input;
            $this->load->view('back/layouts/main', $data);
        } else {
    
            // ...
    
       }
    
    }