I am using dropdownlist in activeform, but i am facing the below issue.
I get the data as an array from the database, but when I display it, even the key value is being displayed. How do I prevent that from happening?
I am getting output as below
0
First
1
Second
I want the dropdownlist to display just the data and not the key values.
First
Second
Below is my code:
$query1 = (new Query())->select('model_name')->from('cars');
$data=$query1->all();
<?= $form->field($model, 'dealer')->dropDownlist($data) ?>
In your code $data is an array of rows and not strings. Try this:
$data = (new Query())->select('model_name')->from('cars')->column();
Doc: Query::column()
Related
I am using Laravel 4.2 and i fetch all locations with this code:
$locations = Location::all();
This Locations are displayed in a select box afterwards. How can i add an additional row to the results in order to show an empty first option in the select box.
The options then should be:
choose a location
location 1
location 2
...
I just want to add an additional item to the result in $locations.
Thanks in advance
You can use:
{!! Form::select('location', ['' => 'Select your location'] + $locations, null , ['class' => 'form-control']) !!}
to update the view. In Laravel 5 there are attribute accessors to append an extra field with your eloquent collection.
Few other ways to do this are:
$locations[null] = 'choose a location';
Form::select('location', $locations);
Form::select('location',[null=>'Please Select'] + $locations);
Another way is to loop through the result and update it. Use json_decode() or 'toArray()` to convert your result into array format.
Otherwise you have to store choose a location as the first row value in your locations table(I know that is inappropriate for the requirement).
You should look at the put method for collections:
https://laravel.com/docs/5.6/collections#method-put
This method is not available in 4.2. You should make a custom Collection class and use it in your model by overwriting the newCollection method. https://laravel.com/docs/4.2/eloquent#collections
Here i like to set preselected value in yii2 dropdownlist,
this is my dropdownlist, how i can set preselect value in this
<?= $form->field($model, 'tpa_email')->dropDownList(
ArrayHelper::map(Approvaldetails::find()->all(),'id','tpa_email'),
['prompt' => 'Select Tpa Email..'])
?>
becoz everytime while updating the form , value getting reset.
The value of $model->tpa_email will be used to select the value from the list items. So make sure that $model->tpa_email contains the key of the value you want to have selected.
As jagsler said you just have to set the key of the dropdown value to model. Try following in your controller:
$model->tpa_email = $key;
Where $key will be the key of the preselected option you want to set.
How do i display the selected value in multiselect while editing and updating the options.
my multiselect function is:
foreach ($subjects as $sub){
$subject_selected[]=$sub['subject_name'];
}
echo form_multiselect('subject[]',$subject_list,'',$js,$subject_selected); ?>
if I m wrong please correct me..
It should be like
echo form_multiselect('subject[]',$subject_list,$subject_selected,$js); ?>
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[].
I have searched for this thing and have failed so many times. But at last find a way around to resolve the issue.
This is the way that I have came across with.
<?php
$manpower_list = array();
$manpowers = $this->db->select('m.id, m.manpower_name')
->from('task_manpower tm')
->join('manpower m', 'tm.manpower_id = m.id')
->where('tm.deleted', 0)->where('m.deleted', 0)
->where('tm.task_id', $model_info->id)
->get()->result();
foreach ($manpowers as $power) {
$manpower_list[] = $power->id;
}
?>
<?php echo form_dropdown("manpower_id[]", $manpower_dropdown, $manpower_list, "class='select2 validate-hidden form-control' id='manpower_id' multiple='multiple'"); ?>
Hope it helps someone.
i'm using codeigniter 2.
i've a mysql table column storing the time taken for each student.
eg. 1.2327, 0.6547, 1.9876
i want to get the max. value that column.
This is my code:
$this->db->select_max('time_taken', 'time');
$result = $this->db->get('students');
echo $result->row()->time;
when i echo the result, it give me a value of 2(correct value should be 1.9876).
What is the correct way to get this value i need, thanks?
Try:
$this->db->select_max('time_taken AS time');
$result = $this->db->get('students')->row();
echo $result->time;
Edit: Make sure your database table field (i.e time_taken) is decimal, NOT integer.
$this->db->select_max('time_taken');
$this->db->from('students');
$query=$this->db->get();
return $query->result_array();
I have an array that returns locations from the database, which I am trying to output into a form drop down using the following code:
<?php form_dropdown('idLocation', $queryLocations, set_value('idLocation'); ?>
The $queryLocations has the locationID and the locationName. And this above code works fine to display all locations, now I need to add an option called 'All' having idLocation as zero to appear at the top of location list.
Can someone please help me do this?
Just prepend the $queryLocations array with your values. Here's one way:
form_dropdown(
'idLocation',
array('0' => 'All') + $queryLocations,
set_value('idLocation')
);
You could probably do this earlier, when you actually create the $queryLocations array as well.
$options[0] = 'All';
foreach ($results as $r) $options[$r->idLocation] = $r->locationName;
Something like that...