Welcome Guest, Not a member yet? Register   Sign In
Trouble Inserting Checkbox Array
#1

[eluser]lenwood[/eluser]
I'm building a blog, and have coded up an administrative page that allows me to insert the blog entry title, date & time, the body itself, and one category. I have a many-to-many database set up, so the only limitation here is my code. I'm not sure how to pass an array within an array, much less have it execute properly.

Here's my view:
Code:
<h2>Write a New Post Here</h2>
&lt;?php echo form_open('admin/blogback/submit_new'); ?&gt;
<p>&lt;input type="text" size="35" name="post_title" /&gt;&lt;/p>
<p>&lt;textarea name="body" id="body"&gt;&lt;/textarea></p>
<p>Select Categories:</p>
<p>&lt;?php foreach($categories as $row): ?&gt;
&lt;input type="checkbox" name="cat_id" value="&lt;?php echo $row-&gt;id; ?&gt;" />&lt;?php echo $row->name; ?&gt;<br />
&lt;?php endforeach; ?&gt;</p>
<p>&lt;input type="submit" value="Submit" /&gt;&lt;/p>
&lt;/form&gt;

The controller:
Code:
function submit_new()
{
    $this->load->model('blogback_model');
    $x = $this->input->post('post_title');
    $ptitle = url_title($x, 'dash', TRUE);
    $data['date_time'] = standard_date('DATE_W3C', time());
    $data['post_title'] = $x;
    $data['post_slug'] = $ptitle;
    $data['body'] = $this->input->post('body');
    $data['cat_id'] = $this->input->post('cat_id');
    $this->blogback_model->submit_entry($data);
    redirect('admin/blogback');
}

And my model:
Code:
function submit_entry($data)
{
    $this->db->set('date_time', element('date_time', $data));
    $this->db->set('post_title', element('post_title', $data));
    $this->db->set('post_slug', element('post_slug', $data));
    $this->db->set('body', element('body', $data));
    $this->db->insert('entries');
    $poca['entry_id'] = $this->db->insert_id();
    $poca['cat_id'] = element('cat_id', $data);
    $this->db->insert('post_cat', $poca);
}

This code works if I select just one category, but if I check more than one then its the bottom-most value that gets saved. Please help.
#2

[eluser]Jinme[/eluser]
I think the problem is that each checkbox should have a different name, as in the following code:
Code:
&lt;input type="checkbox" name="option1" value="Milk"&gt;Milk<br/>
&lt;input type="checkbox" name="option2" value="Butter" checked&gt;Butter<br/>
&lt;input type="checkbox" name="option3" value="Cheese"&gt;Cheese<br/>
You can use in the foreach the next for generate the name:
Code:
name="cat_&lt;?= $row->id ?&gt;"
#3

[eluser]lenwood[/eluser]
If I do that then I'll need to write code to store them all in one array and save them in the cat_id field of my table. Is that the right way?

In a Google search I found that I should add brackets to the name attribute so that it becomes an array. I'm just not sure what to do with it beyond that.

Code:
&lt;input type="checkbox" name="cat_id[]" value="&lt;?php echo $row-&gt;id; ?&gt;" />
#4

[eluser]Jinme[/eluser]
Well, my code is one way. Your code is other way to the same place.
In your case PHP will automatically place the checked boxes into an array if you place [] brackets at the end of each name.
In this case cat_id is a Array and as such should be evaluated, you can use a foreach. Try this:
Code:
$categories = $data['cat_id'];
foreach ($categories as $category) {
   // Print value (only test)
   echo $category;
   // Insert here your code for save in the table
}




Theme © iAndrew 2016 - Forum software by © MyBB