Welcome Guest, Not a member yet? Register   Sign In
Multiple checkbox updating
#1

[eluser]Wonder Woman[/eluser]
I currently have a system set up where I can delete multiple database entries using checkboxes and then submitting the form and now I want to be able to set up a feature where I can publish a number of these entries by setting the publish field in the database to 1 or 0. My problem is that I have the value of the publish checkbox as the entry id so I can't set them up as 1 or 0.

Here is my Controller:

Code:
if($this->input->post('submit') == 'Publish')
{
                   $this->admin_model->publish_competition_entries($this->input->post('publish_entry'));
redirect('admin/entries');
}

And my model:

Code:
function publish_competition_entries($competition_entry) {

for($i=0; $i<count($competition_entry); $i++)
{
$competition_entry_id = $competition_entry[$i];
$competition_entry = $competition_entry[$index];

$this->db->set('published', $competition_entry);
$this->db->where('id', $competition_entry_id);
$query = $this->db->update('competition_entries');
}
}

and my View:

Code:
if($entry['published'] == 1) {
echo '<td>'.form_hidden('is_published', 1).''.form_checkbox('publish_entry[]', $entry['id'], 'checked="TRUE"').'</td>';
} else {
echo '<td>'.form_hidden('is_published', 0).''.form_checkbox('publish_entry[]', $entry['id']).'</td>';
}

If you could help me that would be great, thanks.
#2

[eluser]Wonder Woman[/eluser]
Hi, if anyone could help me with the above I would most appreciate it, thanks.
#3

[eluser]Cristian Gilè[/eluser]
Hi WW,

to set a checkbox as checked the third parameter of form_checkbox function needs to be TRUE or FALSE like this:

Code:
echo form_checkbox('publish_entry[]', $entry['id'], TRUE);

not

Code:
echo form_checkbox('publish_entry[]', $entry['id'], 'checked="TRUE"');

When you submit the form, the post variable contains only the values of checked checkbox.

For example, if you submit a form like this:

Code:
echo form_checkbox('publish_entry[]', 1, TRUE);
  echo form_checkbox('publish_entry[]', 2, FALSE);
  echo form_checkbox('publish_entry[]', 3, TRUE);

the publish_entry[] array contains the first and third value but not the second. (
Code:
Array ( [0] => 1 [1] => 3 )
)
#4

[eluser]Wonder Woman[/eluser]
Hey Cristian Gilè,

Thank you so much for that, it is perfect and it works.




Theme © iAndrew 2016 - Forum software by © MyBB