我想使用form_dropdown(...)填充下拉列表中的名称列表,并相应地获取匹配的名称。
我的模型和控制器类似乎都工作正常,但是我无法将第二个参数传递给视图中的form_dropdown()函数,并得到错误消息“Undefined variable:分支”,我试着转储getBranch()返回的值,它工作了。我有3个不同的PHP脚本用于视图:“index”(显示表单表等中的数据),“form”(我在这里为一个新学生填写详细信息)和“index-datatable”(数据表的Jquery脚本)。请注意,所有视图都在一个目录/文件夹“students”中,如控制器的索引函数所示。我是Codeigniter 3的新手。
控制器类
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);
}
模型类
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);
}
视图(窗体,非索引)
<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>
添加方法
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'));
}
}
***主视图***仅显示加载其他页面的位置。一些脚本被删除,即页眉和页脚脚本。
<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>
1条答案
按热度按时间sqserrrh1#
您没有在
add
函数中向表单模板传递branch
变量。将
branch
添加到if
中,如下所示: