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]
]);
}
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
There's so many question about this but because I'm a newbie, I don't get it at all. so I've successfully saving multiple data into my db. I have 2 tables, TicketPrice as a list of price that visitor would choose, then I will save the selected data into Payment table. This is what I've done in my controller:
$idcategory = $request->id_category;
$price = $request->price;
$qty = $request->qty;
$data = [];
$i = 0;
foreach($idcategory as $key => $value) {
if ($qty[$i] > 0) {
for ($k=0; $k < $qty[$i] ; $k++) {
$data[] = [
"id_category" => $value,
"name" => $request->nama[$key],
"price" => $request->price[$key],
"qty" => $request->qty[$key],
];
}
}
$i++;
}
Payment::insert($data);
$latest = News::orderBy('date', 'DESC')->take(3)->get();
return view('form.conference-regist',compact('latest'))->with('data',$data);
and this is when I try to foreach the selected data (it's multiselect) in my blade after saving it on Payment table:
<?php foreach ($data as $key): ?>
<div class="row">
<div class="col-lg-12 col-md-12 col-xs-12">
<div class="form-group">
<label for="exampleInputEmail1">Price</label>
<input type="text" class="form-control" id="price" aria-describedby="emailHelp" name="price" value="{{$key->price}}">
</div>
</div>
</div>
<?php endforeach; ?>
I wonder if it necessary to put hasMany or belongsTo into the models? Or is there any easier way to solved it?
Well you can try this to get the inserted data back and to pass it to view
$data2 = Payment::insert($data);
$data2 = $data2->fresh(); //this will re-retrieve the inserted data back from table.
$latest = News::orderBy('date', 'DESC')->take(3)->get();
return view('form.conference-regist',compact('latest'))->with('data',$data2);
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', [])),
...
]);
I'm a new user of CakePHP.
I'm trying to add some div to contain my input + my label.
This is what I've got :
<?php
$option = array ("value1" => "labelContent1", "value2" => "abelContent2");
echo $this->Form->input('name', array('type' => 'radio', 'options' => $option, 'div' => true, "legend" => false));
?>
<div class="input radio">
<input type="hidden" name="data[Quiz][name]" id="ModelName_" value=""/>
<input type="radio" name="data[Quiz][name]" id="ModelName1" value="value1" />
<label for="ModelName1">labelContent1</label>
<input type="radio" name="data[Quiz][name]" id="ModelName2" value="value2" />
<label for="ModelName2">labelContent2</label>
</div>
And this what I would like to have :
<div class="input radio">
<input type="hidden" name="data[Quiz][name]" id="ModelName_" value=""/>
<div>
<input type="radio" name="data[Quiz][name]" id="ModelName1" value="value1" />
<label for="ModelName1">labelContent1</label>
</div>
<div>
<input type="radio" name="data[Quiz][name]" id="ModelName2" value="value2" />
<label for="ModelName2">labelContent2</label>
</div>
</div>
Do you know if it is possible to make it by using the FormHelper ?
I know it's an old thread but I had to reply.
The correct way to achieve this without creating a bad mark-up is to use before and after options:
echo $this->Form->input('name', array( 'type' => 'radio',
'separator'=> '</div><div>',
'before' => '<div>',
'after' => '</div>',
'options' => $option,
'label' => true,
"legend" => false
)
);
It looks like you can't achieve that with the Form Helper. The radio options are not wrapped in anything. See code.
You can add 'separator' => '<br/>' to the Form->input options, to display each option in its own line.
or implement it yourself.
you can try this
$option = array ("value1" => "labelContent1", "value2" => "abelContent2");
echo $this->Form->input('name', array( 'type' => 'radio',
'separator'=> '</div><div>',
'options' => $option,
'label' => true,
"legend" => false
)
);
<?php
$option = array ("value1" => "labelContent1", "value2" => "abelContent2");
echo $this->Form->input('name', array(
'type' => 'radio',
'options' => $option,
'templates' => ['radioWrapper' => '<div>{{label}}</div>'],
"legend" => false));
?>
This seems to work well for Cake 3.+
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.