My project is about shops belongs to many sections.
When creating a shop, sections are chosen (selected) in a multi-select list and values are saved correctly. Also when editing the info, user can save changes correctly. The problem is that: in edit form multi-select object is clear (no items are selected at all) while we need to set options to be selected according to the pre-saved data, so user can see and modify it.
in controller I select lists like this:
public function edit($id)
{
$data = Shops::with('sections')->where('id', '=', $id)->get();
$sectionslst = Sections::lists('section_name','id');
return View::make('admin.shops.edit')->with('data',$data)
->with('sectionslst', $sectionslst );
}
and I populate data in my view this way
{{Form::label('shop_section','الاقسام') }}
{{ Form::select('shop_sections[]', $sectionslst, array (1, 2), array ('multiple' => 'multiple' ))}}
//I'm using array(1, 2) just to try how it works, but it doesn't, all items still un-selected :-(
First, you obviously can't use array(1,2) to test it if you don't have those id's in your list. Instead:
{{ Form::select('shop_sections[]', $sectionslst, array(13, 14), array ('multiple' => 'multiple' ))}}
Now the real problem here is that $data->sections is an Eloquent collection of models. However Form::select expects an array of keys. You can fix that by using lists()
$data->sections->lists('id')
Related
I have a dropdown on my site that allows for multiple selections:
$this->Form->input('systems', array('label'=>'System Assignments', 'empty'=>'', 'default'=>'', 'div'=>false, 'multiple'=>true, 'class'=>'chosen',
'options'=>$systems));
This code from my controller populates the $systems variable:
$systems = $this->Discrepancy->find('list', array('fields' => array('id', 'description'),
'conditions'=>array('Discrepancy.deleted_record' => 0),
'order'=>array('Discrepancy.display_order'=>'ASC')));
$this->set(compact('systems'));
When the user makes their selection(s), the row ID's are stored as an array in a table called Users in a field called systems.
$system_string = implode(',', $this->request->data['User']['systems']);
$this->request->data['User']['systems'] = $system_string;
systems
-------
50,22
On my Edit screen, I would like to be able to use that array of values as my list of ID's for retrieving and displaying the user's system choices, by adding a 'value' parameter to the dropdown.
'value'=>array_key($chosen_systems);
How can I use these stored values in a $this->Model->find statement?
Got it. Guess I needed to write it all out first.
Put the following line in the $this->Model->find statement:
'conditions'=>array('Discrepancy.id'=>explode(',',$var_for_systems_from_db))
How to populate a select dropdown in cakephp3 from a database table.
Currently Cakephp produces an array (list of options) which is numeric indexed.
The option values should have id of database records (should not be the numeric keys generated by Cakephp)
Departments(table) is a taxonomy to classify events.
Events(table) which should store department based list of events.
A event may belongs to many departments. How do I achieve this?
EventsController.php
<?php $department_results = $connection->execute('SELECT departmentname FROM department')->fetchAll('assoc'); // list of departments
$this->set('departmentvalues', $department_results );
Events: add.ctp
<?php echo $this->Form->input('department', array('label' => false,
'div' => false,
'type' => 'select',
'multiple'=>'checkbox',
'legend' => 'false',
'options' => $departmentvalues,
'empty' => 'Please select a Department'
));
Objective:
A select dropdown with values from database, option values should be id of the database record
Expected result:
<select name="department"><option value="2">Maths</option><option value="4">Physics</option></select>
Issue:
cakephp generates numeric indexed array of options.
<select name="department"><option value="0">Maths</option><option value="1">Physics</option></select>
You should really use the CakePHP standard for querying your models, instead of raw SQL, especially in the case of a simple list.
Something like the below should do what you need:
$department_results = $this->Departments->find('list')
->hydrate(false)
->fields(['id','departmentname'])
->toArray();
$this->set('departmentvalues', $department_results);
Note that you will need to include the fields as you have named your column departmentname. By default, a find('list') should return id and name fields.
Another option is to set the displayField this way :
`class DepartmentsTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->displayField('departmentname');
}
}`
In the controller you may just call the method find('list') this way :
$departments= TableRegistry::get("Departments");
$departmentvalues=$departments->find('list')
->hydrate(false)
->toArray();
$this->set('departmentvalues', $department_results);
I am using Laravel Collections methods and am trying to key my query results (which are a collection) by the id. The problem is I have multiple entries with the same id, but point to different countries and I want to have all of the values, not just the last one.
Here is my code that i am using so far:
$allCountries = new Collection($allCountries);
$offerCountries = $allCountries->keyBy('id');
dd($offerCountries);
foreach ($offer as $o) {
$o->countries = $allCountries->get($o->id);
}
To explain, my query puts the results in $allCountries which contains ids and countries and those results looks something like this
id=>225, country=>US
id=>225, country=>IT
id=>3304, country=>NZ
Just to give you a quick idea. I want to key this by the id which results in $offerCountries. I then loop thru a previous Collection that contains offers which have a certain ID that relates to the country result by id. So for the offer 225, the countries it contains are US and IT. I loop thru each offer and set the countries object equal to all the $allCountries id that it equals. The problem I have here is keyBy overwrites the value and only takes the last one. I am hoping to get some results like this:
[
225 => countries: {'id' => 225, 'country' => 'US'}, {'id' =>
'225', 'country' => 'IT'}
3304 => ['id' => 3304, 'country' => 'NZ'],
]
Is there a laravel method to do this, or do I need to write my own keyBy so it does not overwrite. If so, how can I get started to write this method?
Thanks
Instead of using keyBy, use groupBy:
$countriesById = collect($allCountries)->groupBy('id');
You could use filter and create a custom filter
$filtered = $allCountries->filter(function ($item) use ($id) {
return $item->id == $id;
});
$filtered->all();
I have this issue:
in Controller:
$countries=mytable::lists('country');
return view('myview', ['countries'=>$countries]);
In view:
{!!Form::select('Country', $countries, $item->Country)!!}
$item->Country is the country of the user and it should be the default option already selected. However, what I get is the list of countries in alphabetical order... as it comes from the database.
My question is what am I doing wrong? Why the select tag doesn't get the default option ?
Thanks.
Try this:
{!! Form::select('Country', $countries, $item->Country, []) !!}
The select input is looking for 4 parameters
The name of the input
The values
The selected value
And an array of other attributes (class, id etc)
What is more, I hope you know, that this only gives you an array of the country names.
$countries = mytable::lists('country')
$item->Country probably gives you a name of the country, whereas the select input has values of 0, 1, 2, 3 and the texts are the names of the countries. So there there isn't a value of a country's name in the select input.
You should edit your question and add the description of mytable so that we'd know how to help you.
As i think, $item->country is an object, you shoud set the id itself
{!!Form::select('Country', $countries, $item->Country->id)!!}
Also in your controller, add id as a key to the collection:
$countries=mytable::lists('country', 'id');
I'm using version 1.1.14 of yii.
My VIEW file has
<?php echo $form->dropDownList($model,'estado', CHtml::listData(Estado::model()->findAll(), 'id', 'estado')); ?>
I have a model called Estado which was generated from a table with only 2 fields ID as PK and estado where I have my data. Which has 3 rows Active, Inactive, Prospecting.
So far the code only shows the last row of that table, ignoring the first 2.
What am I doing wrong?
for the dropdown list you can pass a normal array :
$data = array(
'number1',
'number2',
'number3',
);
or an array with key => value
$data = array(
7 => 'number7',
2 =>'number2',
4 =>'number4',
);
Chtml::listData() will only help you make that array avalable for the function
however if you need to make a combination of models ( or arrays) you have to do that manually using array concatenation functions such as CMap::mergeArray()