ajt
ajt

Reputation: 662

displaying selected checkboxes in cakephp

I want to get the selected checkboxes from a list. I am getting below errors/warnings

paramater 1 not being a string (at for loop).

The checkboxes have a ID field so when a checkbox is selected I simply want to get this ID of that row called checkid.

I cant seem to cycle through the list one at a time and too check what field has been checked.

I have tried all sorts of combinations and I don't know exactly the correct method for this.

I didn't find it here http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

controller

   if (isset($this->request->data['addrecord'])) {   

               foreach ($this->request->data as $key => $item): 



           if (isset($item['checkid'])){
              if ($item['checkid']>0){ 

               // $lessonId=($item['id']);
                debug($item['checkid']); //no output

              }
            }


          endforeach;



view//

    echo '<td>'.$this->Form->checkbox('User.'.$key.'.checkid', 
                       array('value'=>$student['Student']['id'], 'checked'=>0)).'</td>';

output where I cant get the individual checkid

  array(
    'addrecord' => 'Add Email(s)',
    'User' => array(
        'firstname' => 'a',
        'lastname' => '',
        'searchemail' => '',
        'id' => '372',
        (int) 0 => array(
            'checkid' => '216'
        ),
        (int) 1 => array(
            'checkid' => '311'
        ),
        (int) 2 => array(
            'checkid' => '0'
        ),
        (int) 3 => array(
            'checkid' => '0'

Upvotes: 0

Views: 138

Answers (1)

kaklon
kaklon

Reputation: 2432

You can use Hash to extract the values as such:

$data = array(
    'addrecord' => 'Add Email(s)',
    'User' => array(
        'firstname' => 'a',
        'lastname' => '',
        'searchemail' => '',
        'id' => '372',
        (int) 0 => array(
            'checkid' => '216'
        ),
        (int) 1 => array(
            'checkid' => '311'
        ),
        (int) 2 => array(
            'checkid' => '0'
        ),
        (int) 3 => array(
            'checkid' => '0'
        )
    )
);

$checked_ids = Hash::extract($data['User'], '{n}.checkid');

The $checked_ids array will then contain

array(
    (int) 0 => '216',
    (int) 1 => '311',
    (int) 2 => '0',
    (int) 3 => '0'
)

Upvotes: 1

Related Questions