1

Im having some trouble getting on with my first codeigniter project, and i have a feeling the answer is really near...

Im populating a form of inputfields from a database, and when the user submits the form, it should run through the rows of the database, and update the content based on the 'id' of each row.

My form (in the view 'admin.php') looks something like this:

<?php echo form_open('admin/update'); ?>
<?php foreach($content as $row) : ?>

<?php echo form_input('id['. $row->id .'][id]', $row->id); ?>
<?php echo form_input('id['. $row->id .'][order]', $row->order); ?>
<?php echo form_input('id['. $row->id .'][title_text]', $row->title_text); ?>
<?php echo form_textarea('id['. $row->id .'][body_text]', $row->body_text); ?>
<?php

if ($row->visibility == 'visible') {
    echo form_checkbox('id['. $row->id .'][visibility]', 'visibility', TRUE);
} else {
    echo form_checkbox('id['. $row->id .'][visibility]', 'visibility', FALSE);
}

?>

<?php endforeach;?>
<?php echo form_submit('Save Changes', 'Save Changes'); ?>
<?php echo form_close(); ?>

Now, most of this is based on a mix of tutorials and help documents. Here's the code from the controller 'admin', that i call when people click 'Save changes':

function update()
{
    $this->load->model('admin_model');
    $this->admin_model->updateRecords($_POST['id']);
    $this->index();
}

The data is then passed on to the model, wherein the function looks like this:

function updateRecords($data) 
{
    $this->db->insert_batch('content', $data);  
}

This is the only way i have gotten it to somehow work. It inserts the data fine, but adds it in new rows instead of updating the ones already there, according to their unique 'id's.

For good orders sake, here's the columns i have in my mySQL database:

id (PRIMARY and AUTOINCREMENT)
order
title_text
body_text
visibility
origin_date

Thanks alot in advance :)

1 Answer 1

1

Here's how I might do it, I added in some basic error handling; Hope this explains itself:

Model

function updateRecords($records) 
{
    // Method 1
    // If you want to proceed if there is an error
    $errors = array();

    // Method 2
    // If you want to rollback if there is an error
    $this->db->trans_start();

    foreach ($records as $id => $values):

        // Your form fields conveniently match the column names
        // No need to create the data array, we already have it
        // If you want to update `origin_date` just do this:
        // $values['origin_date'] = time();

        // See if checkbox values were sent
        // Assumes visibility is integer
        if (empty($values['visibility']))
        {
            $values['visibility'] = 0;
        }
        else
        {
            $values['visibility'] = 1;
        }

         // Using method 1, store the error id in an array
        $this->db
            ->where('id', $id)
            ->update('content', $values) OR $errors[] = $id;

        // If using method 2, stop the loop
        $this->db
            ->where('id', $id)
            ->update('content', $values) OR break;


    endforeach;

    // Method 1
    // Return the array of failed updates
    // Controller will check if the array is empty
    // You can tell the user which updates failed or just how many
    return $errors;

    // Method 2
    // This will return TRUE or FALSE
    $this->db->trans_complete();
    return $this->db->trans_status();
}

Controller

function update()
{
    $records = $this->input->post('id');
    $this->load->model('admin_model');

    // Make sure to return errors here if they occur

    // Using method 1
    $errors = $this->admin_model->updateRecords($records);
    $num_errors = count($errors);
    if ($num_errors > 0)
    {
        echo "There were {$num_errors} errors!";

        // You can optionally print which records had errors
        echo "There were errors updating these rows: ".implode(', ', $errors);
    }

    // Using method 2
    $success = $this->admin_model->updateRecords($records);
    if ( ! $success)
    {
        echo "Error performing update, no records were saved.";
    }

    $this->index();
}

Don't just echo the errors of course, use some kind of message library, session data, or send variables to the view. Some references:

http://codeigniter.com/user_guide/database/transactions.html

http://codeigniter.com/user_guide/database/active_record.html#update

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.