Welcome Guest, Not a member yet? Register   Sign In
Help required with update and insert
#7

[eluser]LuckyFella73[/eluser]
You could do it like in the code provided. It's code from
an older project but basically it works like that. I deleted as many
rows as possible to make it easier to read for you. Hope it
helps you to set up your controller:

Code:
// method 'edit' in your controller:
function edit()
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    
    if ( isset($_POST['form']) AND $_POST['form'] == 'edit' ) // hidden input field with name='form' and value='edit'
    {
        # Form was send:
        $this->id = $this->input->post('row_id');

        $this->load->helper('form');
        $this->load->library('form_validation');

        # Rules:
        $this->form_validation->set_rules('title', 'Title', 'trim|required');
        $this->form_validation->set_rules('text', 'Text', 'trim|required');



        if ($this->form_validation->run() == FALSE)
        {
        
            $data['row_id'] = $this->input->post('row_id');

            $data['title'] = $this->input->post('title');
            $data['text'] = $this->input->post('text');

            $this->load->view('view_edit', $data);
        }
        else
        {                
            $data_db = array(
                'title' => $this->input->post('title'),
                'text' => $this->input->post('text')
            );
            
            $query = $this->YOUR_model->update($data_db, $this->id);

            if ( $query === FALSE)
            {
                $data['msg'] .= 'failed'; // echo in view file
            }
            redirect('list_entries', 'refresh');
        }
    }
    else
    {
        $this->id = $this->uri->segment(3);
        $data['row_id'] = $this->id;
        
        # Get DB data
        $query = $this->YOUR_model->get_row($this->id);

        if ($query === FALSE)
        {
            echo ("query failed");
            exit();
        }
        if ($query->num_rows() > 0)
        {
            foreach ($query->result() as $row)
            {
                $data['row_id'] = $this->id;
                $data['title'] = $row->title;
                $data['text'] = $row->text;
            }
        }
        else
        {
            echo("Fehler beim Abrufen der zu editierenden Daten aus der Datenbank (DE)");
            exit();
        }
        // send db data to the form
        $this->load->view('view_edit', $data);
    }
}

// view file components:
<input type="hidden" id="row_id" name="row_id" value="<?php echo set_value('row_id', $row_id); ?>" />
<input type="text" name="title" id="title" value="<?php echo set_value('title', $title); ?>"/>
<textarea name="text" id="textarea"><?php echo set_value('text', $text); ?></textarea>


Messages In This Thread
Help required with update and insert - by El Forum - 08-16-2011, 03:42 AM
Help required with update and insert - by El Forum - 08-16-2011, 05:10 AM
Help required with update and insert - by El Forum - 08-16-2011, 05:23 AM
Help required with update and insert - by El Forum - 08-16-2011, 05:30 AM
Help required with update and insert - by El Forum - 08-16-2011, 05:37 AM
Help required with update and insert - by El Forum - 08-16-2011, 05:40 AM
Help required with update and insert - by El Forum - 08-16-2011, 06:11 AM
Help required with update and insert - by El Forum - 08-16-2011, 06:15 AM
Help required with update and insert - by El Forum - 08-16-2011, 06:31 AM
Help required with update and insert - by El Forum - 08-16-2011, 06:41 AM
Help required with update and insert - by El Forum - 08-16-2011, 06:44 AM
Help required with update and insert - by El Forum - 08-16-2011, 06:50 AM
Help required with update and insert - by El Forum - 08-16-2011, 06:52 AM
Help required with update and insert - by El Forum - 08-16-2011, 07:10 AM
Help required with update and insert - by El Forum - 08-16-2011, 07:16 AM
Help required with update and insert - by El Forum - 08-16-2011, 07:22 AM
Help required with update and insert - by El Forum - 08-16-2011, 07:41 AM
Help required with update and insert - by El Forum - 08-16-2011, 09:16 AM
Help required with update and insert - by El Forum - 08-16-2011, 09:19 AM
Help required with update and insert - by El Forum - 08-16-2011, 09:29 AM
Help required with update and insert - by El Forum - 08-16-2011, 09:31 AM
Help required with update and insert - by El Forum - 08-16-2011, 09:44 AM
Help required with update and insert - by El Forum - 08-16-2011, 09:54 AM
Help required with update and insert - by El Forum - 08-16-2011, 10:03 AM
Help required with update and insert - by El Forum - 08-16-2011, 10:08 AM
Help required with update and insert - by El Forum - 08-16-2011, 10:37 AM
Help required with update and insert - by El Forum - 08-16-2011, 11:41 AM
Help required with update and insert - by El Forum - 08-17-2011, 03:02 AM
Help required with update and insert - by El Forum - 08-17-2011, 05:24 AM
Help required with update and insert - by El Forum - 08-17-2011, 06:08 AM
Help required with update and insert - by El Forum - 08-17-2011, 06:16 AM
Help required with update and insert - by El Forum - 08-17-2011, 06:23 AM
Help required with update and insert - by El Forum - 08-17-2011, 06:33 AM
Help required with update and insert - by El Forum - 08-17-2011, 06:34 AM
Help required with update and insert - by El Forum - 08-17-2011, 06:35 AM
Help required with update and insert - by El Forum - 08-17-2011, 06:42 AM
Help required with update and insert - by El Forum - 08-17-2011, 07:41 AM
Help required with update and insert - by El Forum - 08-17-2011, 07:55 AM
Help required with update and insert - by El Forum - 08-17-2011, 08:00 AM
Help required with update and insert - by El Forum - 08-17-2011, 08:06 AM
Help required with update and insert - by El Forum - 08-17-2011, 08:06 AM
Help required with update and insert - by El Forum - 08-17-2011, 08:07 AM
Help required with update and insert - by El Forum - 08-17-2011, 08:08 AM
Help required with update and insert - by El Forum - 08-17-2011, 08:09 AM
Help required with update and insert - by El Forum - 08-17-2011, 08:10 AM
Help required with update and insert - by El Forum - 08-17-2011, 08:13 AM
Help required with update and insert - by El Forum - 08-17-2011, 08:21 AM
Help required with update and insert - by El Forum - 08-17-2011, 08:24 AM
Help required with update and insert - by El Forum - 08-17-2011, 08:41 AM
Help required with update and insert - by El Forum - 08-17-2011, 10:21 AM
Help required with update and insert - by El Forum - 08-17-2011, 10:26 AM



Theme © iAndrew 2016 - Forum software by © MyBB