I have problem with my dropdown. I need dropdown that can select only one value and I use the same dropdown to repeat the selection for others row. Here is my page look like
Click here to view My page
My code for the dropdown is
<?php
for($i=0;$i<=$rNumber;$i++){
$occNo = array(
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'7' => '7',
'8' => '8',
'9' => '9',
'10' => '10'
);
echo form_dropdown('roomPax[]',$occNo,set_value('roomPax', isset($databaseData[$i]->roomPax) ? $databaseData[$i]->roomPax : ''),'class="form-control"');
}
?>
I found that, the dropdown change to multiple select because I use roomPax[] (array name). I have to use array name because the same dropdown I use for other row. How to set this dropdown as single select and maintain roomPax[] as their name?
You do not need to define name as like that: roomPax[]
Just replace it with roomPax in first param of form_dropdown()
From Codeigniter User Guide:
Lets you create a standard multiselect field. The first parameter will
contain the name of the field, the second parameter will contain an
associative array of options, and the third parameter will contain the
value or values you wish to be selected. The parameter usage is
identical to using form_dropdown() above, except of course that the
name of the field will need to use POST array syntax, e.g. foo[].
As per user guide, when you need multiselect options than you can use your select name as an array.
Related
I am using the multi-select box as follows, where I want to keep old values as selected in edit mode.
{!!Form::select('team_id[]',$teams , old('team_id[]') , array('class'=>'custom-select', 'id'=>'team_id', 'multiple'=>true)) !!}
Below is my controller code.
$teams = Teams::select('team_name', 'id as team_id')
->where('team_status_id', 1)
->pluck('team_name', 'team_id');
$teams->prepend('Select Teams', 0);
I have 2 tables. One is teams table, which contains the name of all teams. Other is team_applied table which contains team_id and survey_id. In add form, I want to display all team name in the multi-select box, and in edit mode, I want to show all team ids in team_applied table as selected along with other unselected team names.
Below is teams table:
Below is team_applied table:
Here is my select box which I need values as selected:
For showing multiple item selected at the time of edit, try something like this:
Form::select('size[]',['L' => 'Large', 'M' => 'Medium', 'S' => 'Small'], ['S', 'M'], ['multiple' => 'multiple', 'class' => 'form-control']);
// here ['S', 'M'] is the array that we have to show selected
Reference
how can i get something randomly from a array for example i have this array I want to grab one of those lines randomly how can I do that using PHP?
$quizes = array(
'3-1' => '2',
'4+4' => '8',
'7-5' => '2',
'4+2' => '6',
'9-3' => '6',
'1+2' => '3',
'9+9' => '18',
'3+2' => '5',
'2*3' => '6',
'5*3' => '15',
'6+6' => '12',
'3+4' => '7',
'7-4' => '3',
'6+2' => '8',
'3*2' => '6',
'7+6' => '13',
'1+1' => '2',
'4*4' => '16',
'10-3' => '7'
);
What i have tried
$rand_keys = array_rand($quizes, 2);
echo $quizes[$rand_keys[0]] . "\n";
echo $quizes[$rand_keys[1]] . "\n";
but this only returns results such as 2 7, 15 2, 3 2 and more
Please help thank you
You can randomize the array order and take the first element. The code would look like this:
shuffle($quizes);
Well, you are getting exactly what you are asking for - value that belongs to randomly chosen key.
To get key => value pair execute:
echo $rand_keys[0] . " => " . $quizes[$rand_keys[0]] . "\n";
Of course you can format output however you'd like to.
Each of the "rows" in your code is actually two parts: a key and its corresponding value. For example, in the first row '3-1' is the key and '2' is its value. The => operator indicates this relationship.
The array_rand function you used will return a random key (or set of keys, if you specify the second parameter) from your array.
$key = array_rand($quizes); // returns a random key e.g. '3-1'
Then you can use that key to get the corresponding value,
$value = $quizes[$key]; // returns the value that matches $key
There are various ways to output them from that point. If you want it to look kind of like it does in the code, you can use
echo "$key => $value";
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');
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.
if I have a muli-dimensional array like that I take from a form submit:
$participants = array(
'participant1'=> array(
'name'=>'jim',
'age' => '15',
'grade' => '8th'),
'participant2'=> array(
'name'=>'tom',
'age' => '17',
'grade' => '9th'),
....
);
Is it better two store whole array into one db column named "Participants" or create a separate column in the row for each participant PERFORMANCE wise if i have a maximum number of participants?
using separate column would be better at the point of normalization also if you need only name or age than it would be better you dont need to fetch all