[eluser]A83[/eluser]
I'm building a application where I want some data that have been entered in a form (and saved to a database) to be displayed on the "success" page, most important I need the id of the database entry to be passed on to the "success" page, where I will display a unique link to the specific entry.
Could anyone help me out with this please?
This is my controller as it is now:
Code: <?php
class Greeting extends Controller {
function Greeting()
{
parent::Controller();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
function index()
{
$this->db->where('id', $this->uri->segment(1));
$this->load->library('form_validation');
$this->form_validation->set_message('required', 'Måste finnas');
$this->form_validation->set_message('min_length', 'Minst 2 bokstäver');
$this->form_validation->set_message('xss_clean', 'Inga konstiga tecken');
$this->form_validation->set_message('valid_email', 'Måste vara en korrekt e-post');
$this->form_validation->set_rules('from', 'From', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('from_mail', 'From e-mail', 'trim|required|valid_email');
$this->form_validation->set_rules('to', 'To', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('to_mail', 'To e-mail', 'trim|valid_email');
$this->form_validation->set_rules('msg', 'Message', 'trim');
if ($this->form_validation->run() == FALSE)
{
$data['query'] = $this->db->get('greetings');
$this->load->view('greeting_view', $data);
}
else
{
$this->db->insert('greetings', $_POST);
$data['query'] = ?; // here I want to send the new entry incl the database entry id to the view
$this->load->view('confirm_greeting_view', $data);
}
}
}
Thanks in advance!
[eluser]A83[/eluser]
I guess this forum works that way, you are linked/redirected to the page of the post with I guess the id of the post in the url...
[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');
}
}
[eluser]A83[/eluser]
Thanks for the fast answer!
It looks promising, I've tried to implement it but, when I load the Model the document goes blank.
This is my controller now:
Code: <?php
class Greeting extends Controller {
function Greeting()
{
parent::Controller();
//$this->load->model('Greeting_model', '', TRUE);
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
function index()
{
$this->db->where('id', $this->uri->segment(1));
$data['query'] = $this->db->get('greetings');
$this->load->library('form_validation');
$this->form_validation->set_message('required', 'Måste finnas');
$this->form_validation->set_message('min_length', 'Minst 2 bokstäver');
$this->form_validation->set_message('valid_email', 'Måste vara en korrekt e-post');
$this->form_validation->set_rules('from', 'From', 'trim|required|min_length[2]');
$this->form_validation->set_rules('from_mail', 'From e-mail', 'trim|required|valid_email');
$this->form_validation->set_rules('to', 'To', 'trim|required|min_length[2]');
$this->form_validation->set_rules('to_mail', 'To e-mail', 'trim|valid_email');
$this->form_validation->set_rules('msg', 'Message', 'trim');
if ($this->form_validation->run()) {
$post = array('from' => $this->input->post('from'),
'from_mail' => $this->input->post('from_mail'),
'to' => $this->input->post('to'),
'to_mail' => $this->input->post('to_mail'),
'msg' => $this->input->post('msg'),
'from_greeting' => $this->input->post('from_greeting'),
'ip' => $this->input->ip_address()
);
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('send/' . $this->post->last_id);
}
}
// The form wasn't processed - display it
$this->load->view('greeting_view', $data);
}
}
And the model:
Code: <?php
class Greeting_model extends Model {
var $last_id
function Greeting_model()
{
parent::Model();
}
function save($post = array()) {
if (count($post) > 0) {
$this->db->insert('greetings', $post);
if ($this->db->affected_rows() == 1) {
$this->last_id = $this->db->insert_id();
return TRUE;
}
}
return FALSE;
}
}
[eluser]Michael Wales[/eluser]
Well, my inclination is that you just copy-pasted my code, since in your controller you are referring to classes that I am sure don't exist (for instance, the post class).
I should have been clearer, my original posting was psudeo-code to point you in the correct direction so you could implement your own solution. There are literally a thousand ways this could be accomplished, this is merely one of them.
My recommendation: delete my code, take the concept I explained, and make it your own.
|