CodeIgniter Forums
How to repopulate check box group? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: How to repopulate check box group? (/thread-2537.html)



How to repopulate check box group? - El Forum - 08-10-2007

[eluser]chobo[/eluser]
I've searched around, but I couldn't find a good answer to repopulating check boxes. I have some validation rules on text fields, but the user also has the option to check some check boxes (There are no validation rules on the check boxes).

If the user fills in the text fields erroneously I would like the state of the check boxes to be kept, so they don't have to refill those in when validation on the text fields fails. I'm using a check box group, so my checkbox name is something like checkbox_name[]. I've tried using $this->validation->set_checkbox('checkbox_name[]', 1), but it doesn't seem to do anything.


How to repopulate check box group? - El Forum - 08-11-2007

[eluser]Michael Wales[/eluser]
View the set_checkbox() section at the bottom of the Validation class.

Why is your name an array? Why not make it just a standard var?


How to repopulate check box group? - El Forum - 08-11-2007

[eluser]smith[/eluser]
I think he uses an array so that he can get multiple checkboxes selected inside one var.

Ontopic:
i was using slightly different method to repopulate checkboxes:

example, inside view:
field names are changed, this is only an example.
Code:
<fieldset>
                <legend>Something</legend>
                <SELECT NAME="something">
                &lt;?
                $sql = "SELECT * from table order by field";
                $query = $this->db->query($sql);
        
                foreach ($query->result() as $row)
                {
                    if ($row->field1 == $this->validation->field1)
                    {
                        print "<OPTION VALUE=\"".$row->field1."\" selected>".$row->field2."\n";
                    }
                    else
                    {
                        print "<OPTION VALUE=\"".$row->field1."\">".$row->field2."\n";
                    }
                }
                ?&gt;
                </SELECT>
                </fieldset>

This way i am operating with field values, not checkboxes, and i can easily setup validation to work with values, just like it would work with input fields.
It is easier for me that way, i am not sure if it breaks MVC philosophy...


How to repopulate check box group? - El Forum - 08-11-2007

[eluser]chobo[/eluser]
Unfortunately I need to use an array of check boxes because of getting names and checking the group as a whole. Plus the number of check boxes is not known until a query runs. That looks like a really good solution smith, I'm going to give that a try, thanks Smile


How to repopulate check box group? - El Forum - 08-11-2007

[eluser]Michael Wales[/eluser]
heh - sorry about that. I was trashed last night and I am coming back through the forums this morning reading my replies wondering how I didn't grasp what people were asking. Looks like every single one of my replies last night didn't help at all... Smile


How to repopulate check box group? - El Forum - 08-11-2007

[eluser]chobo[/eluser]
No worries Smile I finally got it to work, but it wasn't with the validation class method set_check(). I had to loop through POST array and check to see if the value from the query matches up with the one in the POST. It's not the prettiest solution but it works.