trOnGAtE

Updating Records
There are thousands of perfectly valid ways to update records. Here we'll just focus on one technique. Please remember, none of this is carved in stone and you are encouraged to add your own ideas.
For those of you who like video tutorials, here's a tutorial covering this topic:
Video Tutorial
In this video I'm going to demonstrate how to update table records
For everyone else, here's what gets covered in the video:
First Steps
Most of the action is going to happen in our create() method, which is on the books controller. Let's kick things off by fetching the record ID from the URL. We can do this by calling upon Trongate's segment() method. For additional security, we're going to immediately convert our record ID into an integer by passing in 'int' as an optional second argument.
$update_id = segment(3, 'int');
Pre-Populating Our Form
Having fetched the ID from the URL, our next goal is to pre-populate our form. If the form has not been submitted and if the update ID is greater than zero then would should fetch the record data from the database. In all other instances, we should fetch the form data by using the _get_data_from_post() method. In PHP, we can achieve this with:
$submit = post('submit');
if (($submit == '') && ($update_id>0)) {
$data = $this->_get_data_from_db($update_id);
} else {
$data = $this->_get_data_from_post();
}
Here's the method for get fetching record data from the database. As you can see, it accepts a record ID:
function _get_data_from_db($update_id) {
$record_obj = $this->model->get_where($update_id);
$data = (array) $record_obj;
return $data;
}
Making The Headline Dynamic
Since our form now has the ability to be used for being creating and updating records, let's make our headline dynamic. We can achieve this passing a 'headline' value into our 'create' view file. With a simple IF / ELSE statement we can change the headline, depending on whether or not the record ID is greater than zero. For bonus 'smug' points you can use a ternary operator and achieve the same thing as an 'IF / ELSE' statement, but with one line of code:
$data['headline'] = ($update_id>0 ? 'Update Record' : 'Create New Record');
Of course, this means that our view file should now echo out the headline, instead of having a head coded headline. For example:
<h1><?= $headline ?></h1>
Performing The Database Update
The actual updating of the database can be carried out by calling upon the 'update()' method, which is part of Trongate's model file.
$this->model->update($update_id, $data);
The Complete Create Method
Below is the code for our complete create() method, which is - of course - on our controller file.
function create() {
//fetch record ID from the URL
$update_id = segment(3, 'int');
//check to see if form has been posted
$submit = post('submit');
if (($submit == '') && ($update_id>0)) {
//fetch the record from the database
$data = $this->_get_data_from_db($update_id);
} else {
$data = $this->_get_data_from_post();
}
$data['headline'] = ($update_id>0 ? 'Update Record' : 'Create New Record');
$data['form_location'] = str_replace('/create', '/submit', current_url());
$data['author_options'] = $this->_get_author_options($data['author_id']);
$data['view_file'] = 'create';
$this->template('admin', $data);
}
The Complete Submit Method
Below is the code for our complete submit() method, which is also to be found on our controller file:
function submit() {
$submit = post('submit');
if ($submit == 'Submit') {
$this->validation_helper->set_rules('title', 'book title', 'required|min_length[3]|max_length[75]');
$this->validation_helper->set_rules('description', 'book description', 'required');
$this->validation_helper->set_rules('author_id', 'author', 'required');
//run the validation tests
$result = $this->validation_helper->run(); //returns true or false
if ($result == true) {
//fetch the posted data
$data = $this->_get_data_from_post();
settype($data['published'], 'int');
$update_id = segment(3, 'int');
if ($update_id>0) {
//update the record
$this->model->update($update_id, $data);
$flash_msg = 'The record was successfully updated';
} else {
//insert into the 'books' database table
$this->model->insert($data);
$flash_msg = 'The record was successfully created';
}
set_flashdata($flash_msg);
redirect('books/manage');
} else {
$this->create();
}
}
}
HELP & SUPPORT
If you have a question or a comment relating to anything you've see here, please goto the Help Bar.