[eluser]Michael Wales[/eluser]
This is how I go about it - by placing a class variable within the model to store the last used ID.
Code:
class Post extends Model {
var $last_id
function save($post = array()) {
if (count($post) > 0) {
$this->db->insert('posts', $post);
if ($this->db->affected_rows() == 1) {
$this->last_id = $this->db->insert_id();
return TRUE;
}
}
return FALSE;
}
}
Code:
class Greeting extends Controller {
function index() {
// Load the form validation library and handle your rules
if ($this->form_validation->run()) {
$post = array('title' => $this->input->post('title'),
'body' => $this->input->post('body'));
if ($this->post->save($post)) {
// Our model will populate the class variable if a post was saved
// We can redirect to a method that will display that post
redirect('view/' . $this->post->last_id);
}
}
// The form wasn't processed - display it
$this->load->view('edit');
}
}