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 :)