当我尝试在codeigniter中将数据更新到db时,我得到表格值为空,而我应该获取之前的条目,以便我可以更新。更新表单应包含可编辑格式的现有值。
这是我的控制器
<?php
class Person extends CI_Controller {
// num of records per page
private $limit = 15;
function __construct() {
parent::__construct();
// $this->load->library('table','form_validation');
$this->load->library('table');
$this->load->library('form_validation');
// $this->form_validation->set_rules();
$this->load->helper('url');
$this->load->model('personModel', '', TRUE);
}
function index($offset = 0) {
$uri_segment = 3;
$offset = $this->uri->segment($uri_segment);
// load data
$persons = $this->personModel->get_paged_list($this->limit, $offset)->result();
// have pagntn
$this->load->library('pagination');
$config['base_url'] = site_url('person/index/');
$config['total_rows'] = $this->personModel->count_all();
$config['per_page'] = $this->limit;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
// generate table data
$this->load->library('table');
$this->table->set_empty(" ");
$this->table->set_heading('No', 'Name', 'Gender', 'Education', 'Date of Birth (dd-mm-yyyy)', 'Interest');
$i = 0 + $offset;
foreach ($persons as $person) {
$this->table->add_row(++$i, $person->name, strtoupper($person->gender) == 'M' ? 'Male' : 'Female',
// ($person->education), date('d-m-Y', strtotime($person->dob)), ($person->interest), anchor('person/view/' . $person->id, 'view', array('class' => 'view', 'onclick' => "return confirm('View Full Details?')")) . ' ' .
($person->education), date('d-m-Y', strtotime($person->dob)), ($person->interest), anchor('person/view/' . $person->id, 'view', array('class' => 'view')) . ' ' .
anchor('person/update/' . $person->id, 'Edit', array('class' => 'update')) . ' ' .
anchor('person/delete/' . $person->id, 'delete', array('class' => 'delete', 'onclick' => "return confirm('Are you sure want to delete this person?')"))
);
}
$data['table'] = $this->table->generate();
$this->load->library('table');
$this->load->library('form_validation');
// load view
$this->load->view('personList', $data);
}
function add() {
// set validation propert/ies
// $this->set_fields();
//echo"hello";
//
// exit;
// set common properties
$data['title'] = 'Add new person';
$data['message'] = '';
$data['action'] = site_url('person/addPerson');
$data['link_back'] = anchor('person/index/', 'Back to list of persons', array('class' => 'back'));
// load view
$this->load->view('personEdit', $data);
}
function addPerson() {
// set common properties
$data['title'] = 'Add new person';
$data['action'] = site_url('person/addPerson');
$data['link_back'] = anchor('person/index/', 'Back to list of persons', array('class' => 'back'));
// set validation properties
$this->set_fields();
$this->set_rules();
//
$this->load->view('personEdit', $data);
}
function view($id) {
// set common properties
$data['title'] = 'Person Details';
$data['link_back'] = anchor('person/index/', 'Back to list of persons', array('class' => 'back'));
// get person details
$data['person'] = $this->personModel->get_by_id($id)->row();
// load view
$this->load->view('personView', $data);
}
function update($id) {
$this->set_fields();
// prefill form values
$person = $this->personModel->get_by_id($id)->row();
$this->form_validation->name = $person->name;
$_POST['gender'] = strtoupper($person->gender);
$this->form_validation->dob = date('d-m-Y', strtotime($person->dob));
$data = array(
'id' => $id,
'name' => $this->input->post('name'),
'gender' => $this->input->post('gender'),
'dob' => $this->input->post('dob'),
'education' => $this->input->post('education'),
'interest' => $this->input->post('interest')
);
// set common properties
// load view
$this->load->view('personEdit', $data);
}
function update_action() {
$this->set_fields();
$id = $this->input->post('emp_id');
// prefill form values
$person = $this->personModel->get_by_id($id)->row();
$data = array(
'name' => $this->input->post('name'),
'gender' => $this->input->post('gender'),
'dob' => $this->input->post('dob'),
'education' => $this->input->post('education'),
'interest' => $this->input->post('interest')
);
$this->db->where('id', $id);
$this->db->update('tbl_person', $data);
redirect('person', 'view');
$data['message'] = '<div class="success">update person success</div>';
echo $data['message'];
}
function delete($id) {
// delete person
$this->personModel->delete($id);
// redirect to person list page
redirect('login/index/', 'refresh');
}
// validation fields
function set_fields() {
// $this->form_validation->set_rules('id', 'name', 'gender','dob','education','interest', 'required');
$fields['id'] = 'id';
$fields['name'] = 'name';
$fields['gender'] = 'gender';
$fields['dob'] = 'dob';
$fields['education'] = 'educaiton';
$fields['interest'] = 'interest';
// $this->validation->set_fields('$fields');
//echo"hellofff";
//exit;
}
// validation rules
function set_rules() {
$rules['name'] = 'trim|required';
$rules['gender'] = 'trim|required';
$rules['dob'] = 'trim|required|callback_valid_date';
$rules['education'] = 'trim|required';
$rules['interest'] = 'trim|required';
$this->form_validation->set_rules($rules);
$this->form_validation->set_message('required', '* required');
$this->form_validation->set_message('isset', '* required');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
}
function form_validation() {
$this->add();
}
// date_validation callback
function valid_date($str) {
if (!ereg("^(0[1-9]|1[0-9]|2[0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})$", $str)) {
$this->form_validation->set_message('valid_date', 'date format is not valid. dd-mm-yyyy');
return false;
} else {
return true;
}
}
}
?>
如何将这些值添加到字段中。
答案 0 :(得分:1)
我假设您使用update()
获取以前的值,使用update_action
将新值添加到上一个值。如果是这种情况,为什么在$this->input->post
函数中使用update()
!只需改变它......
function update($id) {
$this->set_fields();
// prefill form values
$person = $this->personModel->get_by_id($id)->row();
$this->form_validation->name = $person->name;
$_POST['gender'] = strtoupper($person->gender);
$this->form_validation->dob = date('d-m-Y', strtotime($person->dob));
$data = array(
'id' => $id,
'name' => $person['name'], // you can also use $person->name
'gender' => $person['gender'],
'dob' => $person['dob'],
'education' => $person['education'],
'interest' => $person['interest']
);
$this->load->view('personEdit', $data);
}
答案 1 :(得分:0)
您只需要在视图中设置值功能
<?php echo form_input('name',set_value('name', $person->name));?>
这将使您在首次加载表单时成为$person->name;
的默认值,并且在验证失败后,此值仍将保持不变。