0

I am trying to get values in select box as dynamic is not working here is the my controller code below:

public function CreateNewAsset() 
{
    $data['courselist'] = $this->Dashboard_model->getDashboardCourses();
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

    //Validating Name Field
    $this->form_validation->set_rules('topicname', 'Topicname', 'required');

   if ($this->form_validation->run() == FALSE) {
        $this->load->view('template/header');
        $this->load->view('Dashboard/CreateNewAsset');
        $this->load->view('template/footer');
    } else {

        //Setting values for tabel columns           
        $data = array(
            'courseid' => $this->input->post('courseid'),
            'topicname' => $this->input->post('topicname'),
            'refid' => $this->input->post('refid'),
            'startdate' => $this->input->post('startdate'),
            'expectedend' => $this->input->post('expectedend')
        );

        //Transfering data to Model
        $this->Dashboard_model->assetInsert($data);
        $data['message'] = 'Data Inserted Successfully';

        //Loading View
        $this->load->view('template/header');
        $this->load->view('Dashboard/CreateNewAsset', $data);
        $this->load->view('template/footer');
    }
}

Here is the my view below:

<form action="<?php echo base_url();?>Dashboard/CreateNewAsset" method="post">
    <div class="form-row">
        <div class="col-md-3">Course:</div>
        <div class="col-md-9"> 
         <select name="courseid" class="form-control">
               <option value="0">Select Course</option>
            <?php foreach ($courselist as $course) { ?>
                <option value="<?php echo $course->id; ?>"><?php echo $course->coursename; ?></option>
            <?php } ?>
        </select>
        </div>
    </div>
</form>

I have added both getting values and insert in the same function. is this the problem I am not getting values in drop-down can anyone help me what is the mistake and I am getting an error as Undefined variable: courselist

Pradeep
  • 9,667
  • 13
  • 27
  • 34
user200
  • 291
  • 1
  • 4
  • 21
  • In your controller, I don't see you passing `courselist` to the view, which would mean that it won't exist in the view (if that is the controller that loads the HTML in your question). – M. Eriksson Aug 15 '18 at 05:09
  • $this->load->view('Dashboard/CreateNewAsset', $data); that was my view i have passed $data right? – user200 Aug 15 '18 at 05:12
  • Yes, but you `$data`-array doesn't contain `courselist`. You do add `$data['courselist']` on the top of your method, but you then overwrite that variable when you later do: `$data = array( ... )`. – M. Eriksson Aug 15 '18 at 05:13
  • yeah thats waht i didn't understand why it is not taking my coutrselist data on the other hadn it is taking my insert $data array.. – user200 Aug 15 '18 at 05:15
  • so what is the solution for that if i want to get my courselist on the same function? – user200 Aug 15 '18 at 05:16
  • Now I see what you're doing. You're actually not passing `$data` the first time you call that view. – M. Eriksson Aug 15 '18 at 05:17

2 Answers2

1

Change the insert part like this since u overriding $data:

  $insert_data = array(
        'courseid' => $this->input->post('courseid'),
        'topicname' => $this->input->post('topicname'),
        'refid' => $this->input->post('refid'),
        'startdate' => $this->input->post('startdate'),
        'expectedend' => $this->input->post('expectedend')
    );
    $this->Dashboard_model->assetInsert($insert_data);

The whole code should be like this :

NOTE : make sure u have loaded form_validation library and method getDashboardCourses return some data

public function CreateNewAsset() 
{
   $data['courselist'] = $this->Dashboard_model->getDashboardCourses();
   $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
   $this->form_validation->set_rules('topicname', 'Topicname', 'required');

   if ($this->form_validation->run() == FALSE) {
        $this->load->view('template/header');
        $this->load->view('Dashboard/CreateNewAsset',$data);
        $this->load->view('template/footer');
   } 
   else 
   {
      $insert_data = array(
            'courseid' => $this->input->post('courseid'),
            'topicname' => $this->input->post('topicname'),
            'refid' => $this->input->post('refid'),
            'startdate' => $this->input->post('startdate'),
            'expectedend' => $this->input->post('expectedend')
        );
        $this->Dashboard_model->assetInsert($insert_data);
        $data['message'] = 'Data Inserted Successfully';
        $this->load->view('template/header');
        $this->load->view('Dashboard/CreateNewAsset', $data);
        $this->load->view('template/footer');
    }
}
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • In the `else`-block, there's still not `courselist` in the `$insert_data`-array. However, it might not be necessary, depending on what happens in the view (it's the same view that are called in both the if and else). – M. Eriksson Aug 15 '18 at 05:22
  • @padeep hi i have one doubt may i ask – user200 Aug 16 '18 at 14:31
0

Your "View" can't find the courseList variable.

You need to do add courseList to your data array before you pass it to the view with the form.

Eg:-

data['courseList'] = $this->getCourseList(); // Fetch the list of rows

Venkat D
  • 127
  • 1
  • 10