0

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>
8
  • Is the index function in your controller rendering the form that you posted? If not, could you show the controller method that is rendering the form? Commented Mar 14, 2023 at 14:14
  • The controller has a method add that calls the form. But not called in the index. If I tried to call the form in the index I get an error. Maybe you could show me how that can be done. Thanks. Commented Mar 14, 2023 at 14:53
  • I can't help without seeing the code. You could try changing this line in the index function $this->load->view('back/layouts/main', $data); to $this->load->view('students/form', $data); to include the form directly and see if that removes the undefined variable error. If that does work, you're probably doing something in the back/layouts/main template file that causes the variables to be lost. If you need more help, please edit your post and add the code for the add function from the controller and for the back/layouts/main template file. Commented Mar 15, 2023 at 12:57
  • Okay, I shall try the above in a bit. but what code would you like to see please? Commented Mar 15, 2023 at 14:08
  • The code for the add function in the controller and the code for the back/layouts/main template file, please. Commented Mar 15, 2023 at 14:16

1 Answer 1

1

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 {

        // ...

   }

}
Sign up to request clarification or add additional context in comments.

3 Comments

Not sure, I am able to see the exact branch_id in the form data payload but the insert isn't working. I tried using actual id from input form (not from form_dropdown()) and the insert works. Not sure, please is there something I am missing? Been on this for hours now. :(
The name of the branch selectbox is branch, not branch_id, so you probably need to change this line 'branch_id' => $this->input->post('branch_id', true), to 'branch_id' => $this->input->post('branch', true), in the add function. If that does not work, please post the exact database error you're getting.
Thanks for the heads-up. Aside your correction, in the view, the first argument was branch instead of branch_id. Working fine now. Thanks.

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.