0

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;
4
  • var_dump($list_languages) might help a lot :) Commented Jun 12, 2013 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. Commented Jun 12, 2013 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 Commented Jun 13, 2013 at 7:59
  • Thanks, that got me looking in the right place. Code is working now. Commented Jun 13, 2013 at 21:47

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.