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.
Related
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
I am working on a project and I am stuck on this my question is I have one array which is like below
$arr1 = array(
array
(
'id' => '1',
'city' => 'A.bad',
),
array
(
'id' => '2',
'city' => 'Pune',
),
array
(
'id' => '1',
'city' => 'Mumbai',
)
);
and I have to compare the this by id and I want the output like below.
$result = array(
array(
'id'='1',
'city'='A.bad','Mumbai'
),
array(
'id'='2',
'city'='Pune'
)
);
if we have same id as in the first one is A.bad so it will take it and in the third one it has id 1 and city as mumbai so it will combine as id 1 and city as a.bad,mumbai and other records are filtered in same manner.
Loop through the array and generate a new array depending on the id.You can try this -
$new = array();
foreach($arr1 as $array) {
$new[$array['id']]['id']= $array['id'];
// check if the city value set for that id
// if set the concatenate else set with the city name
$new[$array['id']]['city']= (isset( $new[$array['id']]['city'])) ? ($new[$array['id']]['city'] . ',' . $array['city']) : $array['city'];
}
Fiddle
If you are getting that data from database the you can group them in the query also.
I have a model that has fields id_peg, nama_peg, and jabatan_peg. So here's what I wanna do:
Autosuggestion that shows the list ofid_peg as I type in the text field id_peg
When I select the suggested id_peg, the fields nama_peg and jabatan_peg are autocompleted based on that corresponding selected value (retrieved from database)
I've been trying to do the first one and I did it. But I'm stuck at the second one.
This is what I did:
view/melaporkan.php:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name' => 'test1',
'source' => $this->createUrl('search'),
// additional javascript options for the autocomplete plugin
'options' => array(
'showAnim' => 'fold',
'select' => 'js:function(event, ui) {
$('#nama_peg').val(ui.item.nama_peg);
$('#jabatan_peg').val(ui.item.jabatan_peg);
}',
),
));
controllers/sitecontroller.php
public function actionSearch() {
$res = array(0 => array('id' => 1, 'value' => "id_peg"), 1 => array('id' => 2, 'value' => "jabatan_peg"), 2 => array('id' => 2, 'value' => "nama_peg"));
if (isset($_GET['term'])) {
$qtxt = "SELECT id_peg FROM pegawai WHERE id_peg LIKE :id_peg";
$command = Yii::app()->db->createCommand($qtxt);
$command->bindValue(":id_peg", '%' . $_GET['term'] . '%', PDO::PARAM_STR);
$res = $command->queryColumn();
}
echo CJSON::encode($res);
Yii::app()->end();
}
All it did was autosuggest for id_peg. When clicked some random id_peg, nama_peg and jabatan_peg were still empty.
What did I do wrong?
Actually the problem is controllers/sitecontroller.php page.
You are sending only id_peg as JSON format but trying to get as ui.item.jabatan_peg.
It is always better to send your value as
[{"id":"Caprimulgus europaeus","value":"European Nightjar"}] pattern
and get the value ui.item.value at your select function.
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');
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