Welcome Guest, Not a member yet? Register   Sign In
Insert and update in the same view
#11

[eluser]Jonas G[/eluser]
This is how I do it:

Controller add:
Code:
$data['action'] = 'add';
$data['add'] = true;
$this->load->view('admin/blog/add_edit', $data);

Controller edit:
Code:
$data['action'] = 'edit';
$data['blog'] = $this->Blog_model->get_blog($id);
$this->load->view('admin/blog/add_edit', $data);


View:
Code:
<h1>&lt;?= ($action == 'add') ? 'Write a blog' : 'Edit blog' ?&gt;</h1>

&lt;?= validation_errors(); ?&gt;

&lt;form action="&lt;?= site_url('admin/blog/'.$action . '/' . (($action == 'add') ? '' : $blog-&gt;id)) ?&gt;" method="post">

<p>&lt;input type="checkbox" name="published" value="1" &lt;?= set_checkbox('published', 1, ($action == 'add') ? 0 : $blog-&gt;published) ?&gt; /><label for="published">Published</label></p>

<p><label for="author">Author</label><br />
&lt;input type="text" name="author" value="&lt;?= set_value('author', ($action == 'add') ? $this-&gt;session->userdata('firstname').' '.$this->session->userdata('lastname') : $blog->author) ?&gt;" /></p>

<p><label for="headline">Headline</label><br />
&lt;input type="text" name="headline" value="&lt;?= set_value('headline', ($action == 'add') ? '' : $blog-&gt;headline) ?&gt;" /></p>

&lt;input type="submit" name="submit" value="&lt;?= ($action == 'add') ? 'Write blog' : 'Edit Blog' ?&gt;" /&gt;

Please note that this is only part of the code. But I think it's enough for you to get the idea.

Also, I'm quite new to codeigniter so if anyone can see a problem with this way of doing it please tell me.

Oh, and by doing it this way you run into a bug in the form_validation library. See: http://ellislab.com/forums/viewthread/99760/

You can optimize this a little by replacing $action == 'add' with $add in all the ($action == 'add') statements
#12

[eluser]il_dandi[/eluser]
Thanks for help me!!

What do you have inserted in the model? ... simply the insert and update queries?? or other for manage insert/update in the same view...


Why do you have
$data['action'] = 'add';
$data['add'] = true;
Can't you use only action or add for manage the "action": insert or update?

Thanks
#13

[eluser]Jonas G[/eluser]
I have a controller that looks something like this:

Code:
function blog($action=false, $id=false)
    {
        $this->load->model('Blog_model');
        
        $blog_table = $this->config->item('blog_table');
        
        $this->load->view('common/header');
        switch ($action) {
            case 'add':
                $this->load->library('form_validation');
                $this->load->library('textile');
                $this->load->helper(array('form','html'));
                if ($this->form_validation->run('admin_blog') == true) {
                    $data = array(
                                    'headline' => $this->input->post('headline'),
                                    'author' => $this->input->post('author'),
                                    'introtext' => $this->input->post('introtext'),
                                    'text_textile' => $this->input->post('text_textile'),
                                    'text_html' => $this->textile->TextileThis($this->input->post('text_textile')),
                                    'published' => $this->input->post('published'),
                                    'create_time' => date('Y-m-d H:i:s', time()),
                                );
                    if ($this->db->insert($blog_table, $data)) {
                        $this->session->set_flashdata('system_message','Blog added');
                    } else {
                        $this->session->set_flashdata('system_message', 'There was an error creating your blog');
                    }
                    redirect('admin/blog');
                } else {
                    $data['action'] = 'add';
                    $data['add'] = true;
                    $this->load->view('admin/blog/add_edit', $data);
                }
                
                break;
        
            case 'edit':
                $this->load->library('form_validation');
                $this->load->library('textile');
                $this->load->helper(array('form','html'));
                if ($this->form_validation->run('admin_blog') == true) {
                    $data = array(
                                    'headline' => $this->input->post('headline'),
                                    'author' => $this->input->post('author'),
                                    'introtext' => $this->input->post('introtext'),
                                    'text_textile' => $this->input->post('text_textile'),
                                    'text_html' => $this->textile->TextileThis($this->input->post('text')),
                                    'published' => $this->input->post('published'),
                                );
                    if ($this->db->where('id', $id)->update($blog_table, $data)) {
                        $this->session->set_flashdata('system_message','Blog edited');
                        redirect('admin/blog');
                    } else {
                        $this->session->set_flashdata('system_message','There was an error editing your blog');
                        redirect('admin/blog');
                    }

                } else {
                    $data['action'] = 'edit';
                    $data['blog'] = $this->Blog_model->get_blog($id);
                    $this->load->view('admin/blog/add_edit', $data);
                }
                break;


I have both $data['action'] = 'add'; and $data['add'] = true; so I can do ($add) ? 'i add' : 'i edit' instead of doing ($action == 'add') ? 'i add' : 'i edit'. I just haven't gotten around to changing that in the view.

I really should put the insert and update statements into models and if I did that I would put them in to different functions - if that answers your question.




Theme © iAndrew 2016 - Forum software by © MyBB