Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I've got a loop that generates checkboxes, I have it working within the view, but I would like to move it into the controller and then pass the resulting string to the view. The problem is set_checkbox() doesn't seem to remember the values when it's placed in a controller. It does however seem to set the default value.

Edit: This is only an issue when validation fails and I want checkboxes to retain the users selections. Otherwise code is working as expected. I also have a validation rule set.

    $languages_by_name = $this->event_model->get_spoken_languages_by_name();

    // Generate array from model data for form_dropdown()
    $i = 1; 
    $list_languages = '';
    foreach ($languages_by_name as $row) {
        $i == 1 ? $first = TRUE : $first = FALSE; // Check if this is the first radio and precheck it.
        $list_languages .= '<label>' . form_checkbox('spokenLanguages[]', $row->event_spoken_id, set_checkbox('spokenLanguages', $row->event_spoken_id, $first)) . ' ' . $row->name . '</label> ';
        $i++;
    }
    // Pass $list_languages to view
    $this->data['list_languages'] = $list_languages;
share|improve this question
    
var_dump($list_languages) might help a lot :) – Kyslik Jun 12 '13 at 22:37
    
How so? $list_languages is a bunch of checkboxes with the first one checked and working as expected but not remembering the checked values when form submission fails due to validation errors. – Ian Hoar Jun 12 '13 at 22:43
1  
yo need to set new data to the ´$list_languages ´ via ´$this->input->post('spokenLanguages')´, please do var_dump as I asked and edit it in your question, also could you please post whole method in edit? (with validation included). thanks – Kyslik Jun 13 '13 at 7:59
    
Thanks, that got me looking in the right place. Code is working now. – Ian Hoar Jun 13 '13 at 21:47
    
glad to help ;) – Kyslik Jun 14 '13 at 10:40
up vote 1 down vote accepted

Here's working code for the controller. There might be a more elegant way to do this.

    // Generate array from model data for form_dropdown()
    $i = 1; 
    $list_languages = '';
    foreach ($languages_by_name as $row) {
        // Check if there is post data
        if(!$this->input->post('spokenLanguages')) {
            // Set first element to checked
            $i == 1 ? $selection = TRUE : $selection = FALSE; // Check if this is the first radio and precheck it.
        } else {
            // Check if this input is checked
            if(in_array($row->event_spoken_id, $this->input->post('spokenLanguages'))) {
                $selection = TRUE;
            } else {
                $selection = FALSE;
            }   
        }

        $list_languages .= '<label>' . form_checkbox('spokenLanguages[]', $row->event_spoken_id, set_checkbox('spokenLanguages', $row->event_spoken_id, $selection)) . ' ' . $row->name . '</label> ';
        $i++;
    }
    // Pass $list_languages to view
    $this->data['list_languages'] = $list_languages;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.