Welcome Guest, Not a member yet? Register   Sign In
Show just posted form entry
#3

[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');
  }
}


Messages In This Thread
Show just posted form entry - by El Forum - 12-30-2008, 01:54 PM
Show just posted form entry - by El Forum - 12-30-2008, 02:12 PM
Show just posted form entry - by El Forum - 12-30-2008, 02:28 PM
Show just posted form entry - by El Forum - 12-30-2008, 03:05 PM
Show just posted form entry - by El Forum - 12-30-2008, 03:38 PM



Theme © iAndrew 2016 - Forum software by © MyBB