As a programmer me and my client Captain Coralyn will try our luck now with iPhone Developing.
Archive for December, 2009
Cake using Multiple Select Checkboxes
December 2nd, 2009
A sample on how to use multiple select checkbox within cakephp views and controller.
After reviewing the http://api.cakephp.org/file/cake/libs/view/helpers/form.php#method-FormHelperselect
1.) Inserting to database
-View Decleration Example.
1 2 3 4 5 6 7 8 9 10 11 | <?php //add.ctp for example ?> <em>How would you describe your job (mark as many as applies): </em> <?php echo $form->input('describeJob', array('label' => false, 'div' => false, 'type' => 'select', 'multiple'=>'checkbox', 'legend' => 'false', 'options' => array('Physical' => 'Physical','Mental' => 'Mental', 'Stressful' => 'Stressful', 'Easy-going' => 'Easy-going', 'Secure' => 'Secure', 'Non-secure' => 'Non-secure', 'Exhausting' => 'Exhausting', 'Relaxing' => 'Relaxing' ) )); ?> |
-On your controller. Use implode php function and add a seperator that you like, for this example I use ","
1 2 3 4 5 6 | <?php //job_controller @add() function if (!empty($this->data)) { $this->data['Job']['describeJob'] = implode(",",$this->data['Job']['describeJob']); $this->Job->create(); ?> |
2.) Editing part, Retrieving values of the datafield to mark check on those were choosen before.
-View Decleration Example. In this example I used explode function of php directly in the view, notice the "selected" option added in to the $form->input helper, this will get the values, supposed to be check in that field.
1 2 3 4 5 6 7 8 9 10 | <? //edit.ctp for example $piecesJob = explode(",", $this->data['Job']['describeJob']); echo $form->input('describeJob', array('label' => false, 'div' => false, 'type' => 'select', 'multiple'=>'checkbox', 'legend' => 'false', 'options' => array('Physical' => 'Physical','Mental' => 'Mental', 'Stressful' => 'Stressful', 'Easy-going' => 'Easy-going', 'Secure' => 'Secure', 'Non-secure' => 'Non-secure', 'Exhausting' => 'Exhausting', 'Relaxing' => 'Relaxing' ), 'selected' => $piecesJob )); ?> |
That's it