I have a multi-select option and i want to save all record in database, now it's saving only the last one, how can I do that? I need to save multi-select from tags with comma (,) between. .
Here is my controller and what I tried
$news = News::create([
'locale' => Session::get('admin_locale'),
'title' => $request['title'],
'slug' => Slugify::slugify($request['title']),
'news_class' => $request['news_class'],
'description' => $request['description'],
'tag' => $request['tag'],
'tags' => $request->input['tags'],
'category' => 'news',
'category_id' => $request['category_id'],
'metatitle' => $request['title'],
'metadescription' => substr(strip_tags($request['description']), 0, 160),
'image' => $image,
]);
Here is my view:
<div class="row d-flex justify-content-center mt-100 col-md-12 g-mb-30" >
<div class="col-md-12" >
<label class="g-mb-10">Tags</label>
<select id="choices-multiple-remove-button" placeholder="Select" multiple title="Category Talent" name="tags">
#foreach($news as $tag)
<option value="{{ $tag->tag }}">{{ $tag->tag }}</option>
#endforeach
</select> </div>
</div>
To pass multiple values you probably want to rename your input to be an array:
<select ... name="tags[]">
Then on the server side you should receive them as an array under the input tags:
$tags = $request->input('tags', []);
You can join the array elements with implode to get a string representation:
$tags = implode(',', $tags);
PHP Manual - Function Reference - Text Processing - Strings - Functions - implode
$news = News::create([
...
'tags' => implode(',', $request->input('tags', [])),
...
]);
Related
i'm trying to send an array from my laravel view to my controller, but i'm only receiving one part os the data, this is what i have:
<form
method="POST"
action="{{ url('/forms/reports') }}"
>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#foreach ($results as $result)
<input value="{{ $result->code }}" name="code">
<input value="{{$result->name}}" name="name">
<input value="{{$result->user}}" name="user">
<input value="{{$result->number}}" name="number">
#endforeach
<div class="col-xs-9">
</div>
<div class="col-xs-3 container-download">
<button type="submit" class="btn btn-download" id="btn-download" >Download</button>
</div>
</form>
But results has this:
array (
0 => '1',
1 => 'Test Name 1',
2 => 'user1',
3 => '1',
), array (
0 => '2',
1 => 'Test Name 2',
2 => 'user2',
3 => '2',
);
And on the table that i have on my view is showing correctly, the two rows of data.
But when i do the post to receive on my controller the full results array, i only get the second row, when i print it like this:
public function generateExcel(Request $request)
{
$code = $request->input('code');
$name = $request->input('name');
$user = $request->input('user');
$number = $request->input('number');
$users = [$code, $name, $user, $number];
Log::debug($users);
}
And my Log shows me this:
[2020-12-30 12:43:58] local.DEBUG: array (
0 => '2',
1 => 'Test Name 2',
2 => 'user2',
3 => '2',
)
And i don't know if i should push the values first or i'm making another mistake. Can anyone help me with this?
That is happening because you are not sending an array. You will need to do something like this.
#foreach ($results as $result)
<input value="{{ $result->code }}" name="code[]">
<input value="{{$result->name}}" name="name[]">
<input value="{{$result->user}}" name="user[]">
<input value="{{$result->number}}" name="number[]">
#endforeach
I want to insert data from my form into mysql bu I am getting errors. How do I solve this problem.
#foreach($assignments as $item)
<input type="text" name="class[]" value="{{$item->class}}" id="">
<input type="text" name="year[]" value="{{$item->year}}" id="">
<input type="text" name="subject_id[]" value="{{$item->subject_id}}" id="">
<label for="" class="ml-3 mt-2 mb-1">Score</label>
<input type="text" name="score[]" id="" style="width: 20rem;" class="form-control ml-3">
</div>
$class = $request->get("class");
$year = $request->get("year");
$subject_id = $request->get("subject_id");
foreach ($request->get('score') as $key => $value){
SubmitScores::create([
'class' => $key[$class],
'year' => $key[$year],
'subject_id' => $key[$subject_id]
]);
}
#endforeach
The error am getting is:
Trying to access array offset on value of type int
These are backwards:
$key[$class]
$key[$year]
$key[$subject_id]
Those should be $variable[$key]. $key is an integer index, like 0, 1, 2, and $class, $year and $subject_id ($variable) are arrays. So:
foreach ($request->get('score') as $key => $value){
SubmitScores::create([
'class' => $class[$key],
'year' => $year[$key],
'subject_id' => $subject_id[$key]
]);
}
I am trying to add below style..
<div class="rowElem noborder">
<label>Language:</label>
<div class="formRight noSearch">
<select name="select2" class="chzn-select">
<option value="opt1">Choose the Language</option>
<option value="opt2" selected="selected">Kannada</option>
<option value="opt3">Telugu</option>
<option value="opt4">Tamil</option>
</select>
</div>
<div class="fix"></div>
</div>
But in cakephp, I have this code
<?php echo $this->Form->input('language_id', array('class' => 'chzn-select' )); ?>
Please give me the solution..
If I understand what you are asking, this is what you need to do.
In your controller you will create your options array for the select box:
$this->set('languageOptions', array('opt1' => 'Choose Language', 'opt2' => 'Kannada', 'opt3' => 'Telugu', 'opt4' => 'Tamil'));
Then in the view, you create the form:
<div class="rowElem noborder">
<label for="language_id">Language:</label>
<?php echo $this->Form->input('language_id', array('class' => 'chzn-select', 'options' => $languageOptions, 'label' => false, 'div' => array('class' => 'formRight noSearch'))); ?>
<div class="fix"></div>
</div>
$langs = array('opt1' => 'Choose Language', 'opt2' => 'Kannada', 'opt3' => 'Telugu', 'opt4' => 'Tamil');
$this->set(compact('langs')); // if you set options from controller
Then in view try this:
$this->Form->input('language_id', array(
'type' => 'select',
'options' => $langs,
'selected' => 2 // suppose default select Kannada
)
);
Cake Php Select Option Code
Language:
Form->input('language_id', array('class' => 'chzn-select', 'options' => $languageOptions, 'label' => false, 'div' => array('class' => 'formRight noSearch'))); ?>
hi
I'm trying to add a disabled option to a select box with the form helper I use this code to generate an extra empty field, but I want this field disabled.
echo $this->Form->input('User.usertype_id',array('type'=>'select', 'empty'=>'usertype');
this generates:
<div class="input select">
<label for="UserUsertypeId">Usertype</label>
<select name="data[User][usertype_id]" id="UserUsertypeId">
<option value="">usertype</option>
<option value="1">athlete</option>
<option value="2">trainer</option>
</select>
</div>
but I want this:
<div class="input select">
<label for="UserUsertypeId">Usertype</label>
<select name="data[User][usertype_id]" id="UserUsertypeId">
<option value="" disabled="disabled" selected="selected">usertype</option>
<option value="1">athlete</option>
<option value="2">trainer</option>
</select>
</div>
Is there any way to do this simply, or should I just use some js?
If you know the options in advance, you can build the $options array to use in the select menu. This should give you exactly what you want:
$options = array(
array(
'name' => 'usertype',
'value' => '',
'disabled' => TRUE,
'selected' => TRUE
),
'athlete',
'trainer'
);
echo $this->Form->input('User.usertype_id', array('type' => 'select', 'options' => $options));
Or possibly this might work, but I haven't tested it either:
echo $this->Form->input('User.usertype_id', array('type' => 'select', 'empty'=> array('text' => 'usertype', 'selected' => TRUE, 'disabled' => FALSE)));
I know this question was last updated in 2010, but I have the actual answer.
Look at the example from the CakePHP docs:
$options = array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
);
echo $this->Form->select('Model.field', $options, array(
'multiple' => 'checkbox',
'disabled' => array('Value 1')
));
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select
Try this one!!
$operations = [
'' => 'Select Operation',
'query' => 'Query',
'create' => 'Create',
'update' => 'Update',
'upsert' => 'Upsert',
'delete' => 'Delete'
];
echo $this->Form->input('operation[]',[
'type' => 'select',
'options' => $operations,
'class' => 'operation-class',
'id' => 'operation-id-1',
'required' => 'required',
'label' => false,
'disabled' => [''],
'value' => ''
]);
mhmm looks like it isn't possible to add some codeblocks to a comment
so, both of your options generated:
<select name="data[User][usertype_id]" id="UserUsertypeId">
<option value="text">usertype</option>
<option value="selected">1</option>
<option value="disabled"></option>
<option value="1">athlete</option>
<option value="2">trainer</option>
</select>
so this didn't work, but I did it this way:
echo $this->Form->input('User.usertype_id', array('type' => 'select', 'empty'=> array('text" disabled="disabled" selected="selected' => '')));
this generates an option with the value: (" disabled="disabled" selected="selected)
so it becomes:
...
<option value="" disabled="disabled" selected="selected"></option>
...
this is a temporary solution, until I find something better, suggestions are welcome!
I merge the solution of Weptunus and stevelove.
In the controller:
$examples = $this->Test->Examples->find('list');
$this->set('examples', $examples);
In the view
echo $this->Form->input('example_id', array('empty' => array(
'' => array(
'name' => __('Select an Example'),
'value' => '',
'disabled' => TRUE,
'selected' => TRUE
)
))
);
i found this success in cake 1.3
<?php echo $this->Form->input('example', array('type' => 'select','id' => 'id','options' => array('empty "disabled="disabled" selected="selected"' => 'name empty', 'val1' => 'text1', 'val2' => 'text2')); ?>
I am using a Form helper in CakePHP. like
echo $form->input('field', array(
'type' => 'radio','legend'=>$r['Attribute']['label'],
// 'after' => '--after--',
// 'between' => '--between---',
'separator' => '--separator--',
'options' => array('1', '2')
));
which generates me as
<div class="input radio">
<fieldset>
<legend>Gender</legend>
<input type="hidden" value="" id="field_" name="data[field]"/>
<input type="radio" value="0" id="Field0" name="data[field]"/>
<label for="Field0">1</label>--separator--
<input type="radio" value="1" id="Field1" name="data[field]"/>
<label for="Field1">2</label>
</fieldset>
</div>
Is there any way to keep my options that i have received from my Database instead of 1,2
where i tried it with receving my options using
<?php foreach ($viewfields as $r): ?>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function($){
$("#"+<?=$r['Attribute']['id'];?>).each(function() {
type= "<?=$r['Attribute']['type'];?>";
if(type=="radio")
{
var ht = $.ajax({
type: "GET",
url: "http://localhost/FormBuilder/index.php/forms/viewChoices/"+attribute_id,
async: false
}).responseText;
var myObject = eval('(' + ht + ')');
var data = myObject;var j=0;
$.map(data.choices, function(i){ j++;
alert(i.choice);//which alerts as male and female correctly.
return i.choice;});
}
});//each
});
alert(i.choice); alerts the options correctly ..
How to keep these options in the array() of the Form Helper so that to get these options male and female instead of default 1,2
Please suggest me..
Place the options in a key => value array - as shown here : Cake Options
echo $form->input('field', array(
'type' => 'radio',
'legend' => $r['Attribute']['label'],
'separator' => '--separator--',
'options' => array('Male' => 'male', 'Female' => 'female')
));
See how you go.