CAKEPHP 1.2 CHECKBOXES - php

I got a problem with getting multiple values for checkboxes from a column in database.
In my database i got a value of '9,10' in a column
However, in the edit view i only got 9, which mean only the checkbox with the value 9 is checked.
How can i make sure that checkbox for value 9 and 10 is both checked in the edit view
I think I need to explode the value of 9,10. but i don't know how can i do it.
Controller
<?php $categories = $this->Product->Category->find('list',array('conditions' =>
array('parent_id !=' => 0),'order' => array('Category.name ASC')));
$this->set(compact('subcategories'));
?>
View (admin_edit)
<?php
echo $form->create('Product', array('action' =>
'edit','class'=>'cmxform','id'=>'form2','type' => 'file'));
echo $form->input('category_id', array('multiple' => 'checkbox', 'label' =>
false,'validate'=>'required:true','options'=>$categories));
echo $form->end('Save',array('class' => 'btn'));
?>

You can explode it in the model afterFind() callback:
http://book.cakephp.org/2.0/en/models/callback-methods.html#afterfind

Related

How to set a value fetched from database table as selected value in drop down list in cakephp?

Hey everyone I am trying to set selected="selected" for drop down list but cant seem to get to solution.
E.g..
A city is entered in user's table when user chooses to edit his profile the rest of the details comes in text boxes but the city should be displayed in drop down list and the default selected value should match with the city of that user.
Note :- The citites are entered directly in like this
echo $this->Form->input("city",array("type"=>"select","empty"=‌​>"City","options"=>a‌​rray("city1"=>"city1‌​","city2"=>"city2","‌​city3"=>"city3","cit‌​y4"=>"city4")));
You can use default to make any option as selected in cakephp
Try This:
echo $this->Form->input('city', array('type' => 'select', 'options' => array("city1"=>"city1‌​","city2"=>"city2","‌​city3"=>"city3","cit‌​y4"=>"city4"), "default" => "city1"));
You don't need to do anything for selected value. Cakephp render automatically fields name. so if in the user user table city field name is city_id than you need to write below statement:
<?php echo $this->Form->input('city_id', array("label" => false, 'type' => 'select', 'options' => $cities, "empty"=>"Select City", "div" => false )); ?>
But if your data is not rendered from cities table, then you need to write the same column name of the select box:
<select name="city">
<?php foreach ($city_array as $key => $value) { ?>
<option <?php echo ($value == $default) ? 'selected' : '' ?> value="<?php $value ?>"><?php echo $value</option>
<?php } ?>
</select>
The third option to get select value like below:
<?php echo $this->Form->input('city', array(
'type' => 'select',
'options' => array('city1' => 'city1', 'city2' => 'city2', 'city3' => 'city3'),
'selected' => 'city1'
));
?>

inserting selected option from dropdown into db codeigniter

I have been working on a registration form which has a dropdown list populated from an associative array like this:
**<?php
**$options = array(
'cbaringo' => 'Baringo',
'cbomet' => 'Bomet',
'cbungoma' => 'Bungoma',
'cbusia' => 'Busia',
'celgeyo' => 'Elgeyo Marakwet',
'cmigori' => 'Migori',**
?>**
I want to insert the option the user selects into a database as follows
**$data = array(
'scounty' => $this->input->post('counties'),
'ssubcounty' => $this->input->post('subcounty'),
'sname' => $this->input->post('dschoolname'),
'skcpecode' => $this->input->post('dkcpecode'),
'stelno' => $this->input->post('dtelno'),
'steampatronname' => $this->input->post('dpatroname'),
'steampatronemail' => $this->input->post('dpatronemail'),
'steampatrontelno' => $this->input->post('dpatrontelno'),
's_password' => $this->input->post('scpassword')
);**
pupulated the dropdown like this:
**echo form_dropdown('counties', $options, 'cdefault');**
The above line displays the options on the dropdown list as expected
//inserted data into db
**$this->my_model->insert_data($data);**
however on inserting the key instead of value from the associative array is inserted into db. Whats the problem?
what you see in a select list is different then the value that is passed from it
$options =
// the value // what the user sees in the drop down
array(
'cbaringo' => 'Baringo',
'cbomet' => 'Bomet',
'cbungoma' => 'Bungoma',
'cbusia' => 'Busia',
'celgeyo' => 'Elgeyo Marakwet',
'cmigori' => 'Migori',**
?>
so if they choose Baringo, the value passed should be cbaringo

Auto populate dropDownListRow form field in yii from database

I have a form having some multi select dropdowns in yii. I have to auto populate the selected values in that multi select box when comes to edit the data. I am fetching the data and passing it inot view file, but am not sure how can i populate the values in the dropdown. please help me
Here is the code for the multi select drop down
echo $form->dropDownListRow($modelDomain, 'domain_id', $domain, array('title' => Yii::t('main', 'SELECT_DOAMIN'),'multiple'=>true ,'style' => 'width:250px;height:150px;'));
For multiple select you need to use 'options' parameter:
echo $form->dropDownListRow($modelDomain, 'domain_id', $domain, array('title' => Yii::t('main', 'SELECT_DOAMIN'),'multiple'=>true ,'style' => 'width:250px;height:150px;', 'options' => array(
1 => array('selected' => 'selected'),
2 => array('selected' => 'selected'),
)));
Where 1 and 2 are domain ids taken from the $_POST;
You can do this in the action:
$post = $this->getRequest()->getPost('ModelName');
$selectedDomains = $post['domain_id']; // this should be an array or selected values
$selected = array_fill(0, count($selectedDomains), array('selected' => 'selected')); // this constructs the 'options' value from view
$selectedOptions = array_combine($selectedDomains, $selected) // this is the 'options' array with selected values as keys and htmlOptions as values
Also, the code is untested and you need to do your validations and other logic stuff yourself.

Cakephp adding option field with array values

I am trying to create a drop down menu (option) and to fill this drop down i have sent an array list to the view:
$country = $this->country_list;
$this->set(compact('country'));
Now my question is does cake have a buildin method for me to set an input field using ($this->Form->input()) with the data of the array list?
take this example
$sizes = array(
's' => 'Small',
'm' => 'Medium',
'l' => 'Large'
);
echo $this->Form->input('size', array('options' => $sizes, 'default' => 'm'));
In the controller, set the value
$this->set('countries', $this->Country->find('list', array('fields' => 'Country.name')));
To show the dropdown box in the view
$this->Form->input('country_id');

CakePHP MySQL Values

echo $this->Form->input('quantity', array('options' => array('1','2','3','4')));
I have this code to input the values 1,2,3 or 4 into the quantity field in my database.
If a user selects 1 it inputs 0, 2 inputs 1 etc.
If the code is
echo $this->Form->input('quantity');
The user can input what they like and guess what...
It works. What am I doing wrong?
I think you're trying to implement a drop-down box. I think you can try this:
$options = array('1' => '1', '2' => '2', '3' => '3', '4' => '4');
echo $this->Form->input('quantity', array('options' => $options, 'default' => '1'));
this will create a select drop down box.
For detail please see here.

Categories