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;
}
}
}
?>
これらの値をフィールドに取得するにはどうすればよいですか。