【问题标题】:Displaying values in select box dynamically is not working in codeigniter在选择框中动态显示值在 codeigniter 中不起作用
【发布时间】:2023-03-22 11:34:02
【问题描述】:

我正在尝试在选择框中获取值,因为动态不起作用 下面是我的控制器代码:

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

以下是我的看法:

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

我在同一个函数中添加了获取值和插入。这是我在下拉列表中没有得到值的问题谁能帮助我是什么错误 我收到一个错误Undefined variable: courselist

【问题讨论】:

  • 在您的控制器中,我没有看到您将 courselist 传递给视图,这意味着它不会存在于视图中(如果这是在您的控制器中加载 HTML 的控制器)问题)。
  • $this->load->view('Dashboard/CreateNewAsset', $data);这是我的观点,我已经通过了 $data 对吗?
  • 是的,但是你的$data-array 不包含courselist。您确实在方法顶部添加了$data['courselist'],但是稍后您会覆盖该变量:$data = array( ... )
  • 是的,我不明白为什么它没有把我的 coutrselist 数据放在另一个上,如果它正在使用我的插入 $data 数组..
  • 如果我想让我的课程列表使用相同的功能,解决方案是什么?

标签: php mysql codeigniter codeigniter-3 codeigniter-database


【解决方案1】:

像这样更改插入部分,因为你覆盖了$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);

整个代码应该是这样的:

注意:确保您已加载 form_validation 库和方法 getDashboardCourses 返回一些数据

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

【讨论】:

  • else-block 中,$insert_data-array 中仍然没有courselist。但是,这可能不是必需的,具体取决于视图中发生的情况(在 if 和 else 中调用的视图相同)。
  • @padeep 嗨,我有一个疑问可以问一下
【解决方案2】:

您的“视图”找不到courseList 变量。

您需要先将courseList 添加到您的data 数组中,然后再将其与表单一起传递给视图。

例如:-

data['courseList'] = $this->getCourseList(); // 获取行列表

【讨论】:

    猜你喜欢
    • 2015-10-10
    • 2021-11-22
    • 2015-06-16
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 2020-07-03
    相关资源
    最近更新 更多