I have cloned HTML prepared with input group. Some checkbox and radio are not selected. I want to push into the selected email.
I've tried many methods.
array_merge()
array_combine()
array_push()
array_merge_recursive()
But none of these worked. Or I don't know how to get them to work.
HTML
<div class="form-group">
<label for="name">E-posta</label>
<div class="input-group">
<input type="email" class="form-control email-address" name="email[]" placeholder="E-Posta giriniz">
<div class="btn-group">
<label class="btn btn-default">
<input class="primary-radio" type="radio" name="primary[]" checked autocomplete="off"> <span
class="fas fa-star"></span>
</label>
<label class="btn btn-default">
<input class="ban-checkbox" type="checkbox" name="ban[]" autocomplete="off"> <span
class="fas fa-ban"></span>
</label>
<label class="btn btn-default">
<input class="invalid-checkbox" type="checkbox" name="invalid[]" autocomplete="off"> <span
class="fas fa-exclamation-circle"></span>
</label>
</div>
</div>
</div>
PHP
public function add(Request $request)
{
$emails = $request->input('email');
$primary = $request->input('primary');
$ban = $request->input('ban');
$invalid = $request->input('invalid');
$emailAddresses = array();
foreach ($emails as $emailKey => $emailValue) {
$emailAddresses[$emailKey] = [
'emailAddress' => $emailValue,
'invalid' => $invalid,
'lower' => $emailAddresses,
'ban' => $ban,
'primary' => $primary
];
}
dd($emailAddresses);
}
Output
array:3 [▼
0 => array:5 [▼
"emailAddress" => "dodis#live.com"
"invalid" => null
"lower" => "dodis#live.com"
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
"primary" => array:1 [▼
0 => "on"
]
]
1 => array:5 [▼
"emailAddress" => "test#live.com"
"invalid" => null
"lower" => "test#live.com"
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
"primary" => array:1 [▼
0 => "on"
]
]
2 => array:5 [▼
"emailAddress" => "bundayok#live.com"
"invalid" => null
"lower" => "bundayok#live.com"
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
"primary" => array:1 [▼
0 => "on"
]
]
]
Such an output comes. It shouldn't be like that. Here's the example I want it to be.
0: {
emailAddress: "bilgi#ekinciler.com.tr"
invalid: false
lower: "bilgi#ekinciler.com.tr"
optOut: false
primary: true
}
1: {
emailAddress: "muhasebe#ekinciler.com"
invalid: false
lower: "muhasebe#ekinciler.com"
optOut: false
primary: false
}
I am sorry for my English. I hope I can. I would be glad if you help.
Try below code,
What I have done is added array index to each loop to get correct value of that index, here your
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
is array itself so you need 0 index for the first array data. and same applies for all the array.
public function add(Request $request)
{
$emails = $request->input('email');
$primary = $request->input('primary');
$ban = $request->input('ban');
$invalid = $request->input('invalid');
$emailAddresses = array();
$i = 0;
foreach ($emails as $emailKey => $emailValue) {
$emailAddresses[$emailKey] = [
'emailAddress' => $emailValue,
'invalid' => $invalid[$i]?$invalid[$i]:'',
'lower' => $emailAddresses,
'ban' => $ban[$i]?$ban[$i]:'',
'primary' => $primary[$i]?$primary[$i]:''
];
$i++;
}
dd($emailAddresses);
}
Related
I have a ecommerce website which shows multiple variants like size, color etc...I have create a products details array as below.
array:1 [▼
0 => array:12 [▼
"proId" => 2268
"name" => "PAMPERS BABY DRY PANTS LARGE -64 PANTS"
"oprice" => 1099.0
"ofprice" => 1044.05
"slug" => "pampers-pants-large-64s-9-14kg"
"variants" => array:5 [▼
0 => array:2 [▼
"title" => "NOS"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 567
"items_value" => "42 NOS"
"sku" => "1058898"
"ean" => "4902430645577"
"mrp" => 699.0
"mfp" => 664.05
"combo" => ""
]
]
]
1 => array:2 [▼
"title" => "NOS"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 568
"items_value" => "24 pants"
"sku" => "PM50"
"ean" => "4902430900485"
"mrp" => 399.0
"mfp" => 379.05
"combo" => ""
]
]
]
2 => array:2 [▶]
3 => array:2 [▶]
4 => array:2 [▼
"title" => "Weight"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 591
"items_value" => "100gm"
"sku" => "wet"
"ean" => "346"
"mrp" => 436.0
"mfp" => 33.0
"combo" => ""
]
]
]
]
]
]
The code for the above is,
$product = Products::select('products.*', 'brand.name', 'category.id as cat_id')->join('category', 'category.id', '=', 'products.category_id')->join('subcategory', 'subcategory.id', '=', 'products.subcategory_id')->join('brand', 'brand.id', '=', 'products.brand_id', 'left')->where(array('products.slug' => $slug, 'products.status' => 1))->get();
$datas = [];
foreach ($product as $pro) {
$item = [
"proId" => $pro->id,
"name" => $pro->title,
"oprice" => $pro->org_price,
"ofprice" => $pro->off_price,
"slug" => $pro->slug,
"variants" => []
];
$variantlist = Provariants::select('products_variants.id', 'variants.name', 'products_variants.variants_type_id', 'products_variants.sku', 'products_variants.ean', 'products_variants.price', 'products_variants.mfp', 'products_variants.combo')->join('variants_type', 'variants_type.id', '=', 'products_variants.variants_type_id')->join('variants', 'variants.id', '=', 'variants_type.variants_id')->where(array('products_variants.products_id' => $pro->id, 'products_variants.status' => 1))->get();
foreach ($variantlist as $list) {
$typelist = Variantstype::where(array('id' => $list->variants_type_id, 'status' => 1))->get();
$subitems = [];
foreach ($typelist as $type) {
$subitems[] = [
"items_id" => $list->id,
"items_value" => $type->name,
"sku" => $list->sku,
"ean" => $list->ean,
"mrp" => $list->price,
"mfp" => $list->mfp,
"combo" => $list->combo
];
}
$item['variants'][] = [
"title" => $list->name,
"items" => $subitems,
];
}
$datas[] = $item;
}
Now the problem is, the title is showing multiple times and one items displaying under it. To get an idea check below image.
https://i.stack.imgur.com/tLjc9.png
I want title to be shown once and all the items under each title should be place together. Please check the required output.
https://i.stack.imgur.com/RFddf.png
Current result code is,
#if (count($pro['variants']) > 0)
#foreach ($pro['variants'] as $variants)
<div class="variants mt-4">
<div>
<h6>{{ $variants['title'] }}</h6>
#foreach ($variants['items'] as $var_items)
<p>{{ $var_items['items_value'] }}</p>
#endforeach
</div>
</div>
#endforeach
#endif
How can I achieve the required output?
A similar solution to the one by #sta, but I think this might be a little more flexible.
#php
$title = '';
#endphp
#foreach ($pro['variants'] as $variant)
<div class="variants mt-4">
<div>
#if($title !== $variant['title'])
<h6>{{ $variant['title'] }}</h6>
#endif
#foreach($variant['items'] as $var_item)
<p>{{ $var_item['items_value'] }}</p>
#endforeach
</div>
</div>
#php
$title = $variant['title'];
#endphp
#endforeach
I have not tested this, but it should write out the title every time $variant['title'] changes.
This will work :
#php
$x = 1;
#endphp
#foreach ($pro['variants'] as $variants)
<div class="variants mt-4">
<div>
#if($x == 1)
<h6>{{ $variants['title'] }}</h6>
#endif
#php
$x += $x;
#endphp
#foreach ($variants['items'] as $var_items)
<p>{{ $var_items['items_value'] }}</p>
#endforeach
</div>
</div>
#endforeach
Above code tested here
I want to view the loop of array in blade view, in my view i want print the array items.
I tried with this but it doesn't work
<div class="time-picker-container">
<div class="time-picker">
<ul>
#foreach($slots as $key => $slot)
<li>
<label class="time-picker-toggle-wrapper">
<input type="radio" value="" name="time-picker" />
<span class="time-name">{{ $slot->value }}</span>
</label>
</li>
#endforeach
</ul>
</div>
</div>
the array:
array:55 [▼
0 => array:2 [▼
"value" => "08:15"
"time-name" => "08:15 AM"
]
1 => array:2 [▼
"value" => "08:30"
"time-name" => "08:30 AM"
]
2 => array:2 [▼
"value" => "08:45"
"time-name" => "08:45 AM"
]
3 => array:2 [▼
"value" => "09:00"
"time-name" => "09:00 AM"
]
It's an array not object so you need to do {{ $slot['value'] }} not {{ $slot->value }}
So there are tons of questions like this, but I haven't found an answer the suits my desired outcome.
consider the following array:
array:13 [
"Peripheral Neuropathy" => array:3 [
"name" => "peripheral_neuropathy"
"value" => array:1 [
0 => "yes"
]
"dependencies" => array:5 [
"neuropathy_staging_fap" => array:1 [
0 => "2"
]
"neuropathy_staging_pnd" => array:1 [
0 => "II"
]
"specify_type" => array:1 [
0 => "Sensory"
]
"autonomic" => array:1 [
0 => "yes"
]
"sensory_fiber_size" => array:1 [
0 => "Small"
]
]
]
"Neuropathic pain" => array:3 [
"name" => "neuropathic_pain"
"value" => array:1 [
0 => "yes"
]
"dependencies" => []
]
"Functional Motor Assessment" => array:3 [
"name" => "functional_motor_assessment"
"value" => array:1 [
0 => "Yes"
]
"dependencies" => array:3 [
"functional_motor_assessment_test_name" => "sample"
"functional_motor_assessment_test_score" => "10"
"functional_motor_assessment_date" => "2020-02-11"
]
]
"Assessment name" => array:3 [
"name" => "functional_motor_assessment_test_name"
"value" => "sample"
"dependencies" => []
]
"Assessment Score" => array:3 [
"name" => "functional_motor_assessment_test_score"
"value" => "10"
"dependencies" => []
]
"Assessment Date" => array:3 [
"name" => "functional_motor_assessment_date"
"value" => "2020-02-11"
"dependencies" => []
]
"Carpal Tunnel Syndrome" => array:3 [
"name" => "carpal_tunnel_syndrome"
"value" => array:1 [
0 => "yes"
]
"dependencies" => []
]
"EMG" => array:3 [
"name" => "emg"
"value" => array:1 [
0 => "yes"
]
"dependencies" => array:5 [
"emg_type" => array:1 [
0 => "Median"
]
"median_amplitude" => "10"
"median_CV" => "10"
"median_tml" => "10"
"median_size" => array:1 [
0 => "7cm"
]
]
]
"EMG Type" => array:3 [
"name" => "emg_type"
"value" => array:1 [
0 => "Median"
]
"dependencies" => array:4 [
"median_amplitude" => "10"
"median_CV" => "10"
"median_tml" => "10"
"median_size" => array:1 [
0 => "7cm"
]
]
]
"Amplitude" => array:3 [
"name" => "median_amplitude"
"value" => "10"
"dependencies" => []
]
"CV" => array:3 [
"name" => "median_CV"
"value" => "10"
"dependencies" => []
]
"TML" => array:3 [
"name" => "median_tml"
"value" => "10"
"dependencies" => []
]
"Size" => array:3 [
"name" => "median_size"
"value" => array:1 [
0 => "7cm"
]
"dependencies" => []
]
]
As we can see there are duplicates, for example look at: Functional Motor Assessment under dependencies - this is correct, but directly under Functional Motor Assessment is Assessment name with the name functional_motor_assessment_test_name.
This is the duplicate. This specific array, Assessment name should not exist because the name already exists in Functional Motor Assessment's dependencies array.
So I thought, I will write the following function:
protected function alreadyExists(array $values, string $fieldName) {
if (empty($values)) {
return false;
}
foreach ($values as $key => $value) {
foreach ($value as $k => $v) {
if ($k === 'dependencies' && !empty($value[$k])) {
return array_key_exists($fieldName, $value[$k]);
}
}
}
return false;
}
where $value is the above array and in this case $fieldName would be (for example) functional_motor_assessment_test_name.
The idea here is that this should walk through the array looking for any key that matches: functional_motor_assessment_test_name and return true if found or false if not (false if the (above) array is empty).
Theres a couple rules:
Return false if the value array is empty, because obviously it wont exist.
Return false if never found
Skip the check if the the dependencies array is empty (it can be empty sometimes).
This is where it should move on to the next array, so if not found in Peripheral Neuropathy move on to Neuropathic pain and so on ...
I think this function has to be recursive, but I am not sure where to put the recursive aspect, to say: well I didn't find it in Peripheral Neuropathy, let's check Neuropathic pain and so on and so forth. Until it is either found or not.
I tried array_walk_recursive, but as I expected while reading the docs, there is no way to break from that kind of function - so I thought, this function I have is on the right track, I just need to make it recursive.
Ideas?
There's no need to loop over the second level arrays, just get the dependencies element directly with indexing.
public function alreadyFound(array $values, string $fieldName) {
foreach ($values as $item) {
if (!empty($item['dependencies']) && array_key_exists($fieldName, $item['dependencies'])) {
return true;
}
return false;
}
If there can be dependencies nested within dependencies, you do need a recursive solution.
protected function alreadyExists(array $values, string $fieldName) {
if (array_key_exists($fieldName, $values)) {
return true;
}
foreach ($values as $item) {
if (!empty($item['dependencies']) && $this->alreadyExists($item['dependencies'], $fieldName) {
return true;
}
}
return false;
}
If the user is doing a registration in a conference, and is registering 1 participants in the registration type with id "1" (Jake K), and 1 participant in the regitration type with id "4" (John W), the request with the form data has this 3 arrays:
"name" => array:2 [▼
1 => array:1 [▼
1 => "Jake"
]
4 => array:1 [▼
1 => "John"
]
]
"surname" => array:2 [▼
1 => array:1 [▼
1 => "K"
]
4 => array:1 [▼
1 => "W"
]
]
"answer" => array:1 [▼
1 => array:1 [▼
1 => array:2 [▼ // answers answered for the registration_type_id 1
1 => "answer1"
2 => "answer2"
]
]
]
Is necessary to use the info of this array to insert in the participants and answers table. Its inserting correctly in the participants table but for the answers table is not working:
foreach ($request->all()['name'] as $key => $nameArray) {
foreach ($nameArray as $nameKey => $name) {
// this is working
$participant_result = Participant::create([
'name' => $name,
'surname' => $request['surname'][$key][$nameKey],
'registration_id' => $registration->id,
'registration_type_id' => $key
]);
// save answer to Database if exist
// this is not working
$answer = Answer::create([
'question_id' => $request['answer'][$key], // the issue is here
'participant_id' => $participant_result->id,
'answer' => $request['answer'][$key][$nameKey], // and here
]);
}
}
The issue is because is necessary to insert in the question_id and answer columns of the answers table. But this values are not returned correctly from the array with "$request['answer'][$key]" and "$request['answer'][$key][$nameKey]".
Do you know how to properly get the question id and answer from the array?
In this above array example, the question id of the "answer1" is 1 and the id of the "answer2" is 2.
"answer" => array:1 [▼
1 => array:1 [▼
1 => array:2 [▼
1 => "answer1"
2 => "answer2"
]
]
Form:
<form method="post"
action="https://proj.test/conference/1/conf-test/registration/storeRegistration">
<h6> Participant - 1 - general</h6>
<div class="form-group">
<label for="namegeneral_1">Name</label>
<input type="text" required id="namegeneral_1" name="name[1][1]" class="form-control" value="">
</div>
<div class="form-group">
<label for="surnamegeneral_1">Surname</label>
<input type="text" id="surnamegeneral_1" class="form-control" name=" surname[1][1]" value="">
</div>
<div class="form-group">
<label for="participant_question">Question 1</label>
<input type='text' name='answer[1][1][1]' class='form-control' required>
<input type="hidden" name="question_required[1][1]" value="1">
</div>
<div class="form-group">
<label for="participant_question">Question 2</label>
<textarea name='answer[1][1][2]' class='form-control' rows='3' required></textarea>
<input type="hidden" name="question_required[1][2]" value="1">
</div>
<h6> Participant - 1 - plus</h6>
<div class="form-group">
<label for="nameplus_1">Name</label>
<input type="text" required id="rnameplus_1" name="rname[4][1]" class="form-control" value="">
</div>
<div class="form-group">
<label for="surnameplus_1">Surname</label>
<input type="text" id="rsurnameplus_1" class="form-control" name=" rsurname[4][1]" value="">
</div>
<input type="submit" class="btn btn-primary" value="Register"/>
</form>
Firstly: in loop you have more than one name, but answer has one key (1). On second run you have $key == 4, but not have this in $request['answer']. Maybe inside first loop you shoud have two loops? One for registering participants and one for registering answers? If answers are registered for both, build array with $participantResult and for every answer register count($participantResult) answers :O If not, remember ID only for first registered participant and save answer.
Please tell me how $request['answer'] works? What is $request['answer'][1] and what $request['answer'][1][1]?
Secondly $request['answer'][$key][$nameKey] in this case is array of two elements. If Answer::create knows about it - it's no problem ;)
EDIT
Is this right solution?
<?php
$aPID = [];
foreach ($request->all()['name'] as $key => $nameArray) {
foreach ($nameArray as $nameKey => $name) {
$aPID[$key] = Participant::create([
'name' => $name,
'surname' => $request['surname'][$key][$nameKey],
'registration_id' => $registration->id,
'registration_type_id' => $key
]);
}
}
foreach($request['answer'] as $pid => $answersArray) {
foreach($answersArray as $questionId => $answer) {
$answer = Answer::create([
'question_id' => $questionId,
'participant_id' => $aPID[$pid],
'answer' => $answer,
]);
}
}
I want to store data from a form into two tables on database when the radio button value on my view is "new", but the below problem was happened. But if the value is "existing", it's works fine.
what wrong with my code ?
Array to string conversion (SQL: insert into customers (company_name, address, service_id, tc_name, tc_dept, tc_phone, tc_email, bill_name, bill_dept, bill_phone, bill_email, updated_at, created_at) values (PT Bank ABC, JL Sudirman, 1, Budi, Technical, 0812345678, budi#abc.co.id, Joko, Finance, 08123456789, joko#abc.co.id, 2016-12-14 11:21:26, 2016-12-14 11:21:26))
here my store code
if($request->select_data == 'new'){
$customer = New Customer;
$customer->company_name = $request->company_name;
$customer->address = $request->address;
$customer->service_id = $request->service_id;
$customer->tc_name = $request->tc_name;
$customer->tc_dept = $request->tc_dept;
$customer->tc_phone = $request->tc_phone;
$customer->tc_email = $request->tc_email;
$customer->bill_name = $request->bill_name;
$customer->bill_dept = $request->bill_dept;
$customer->bill_phone = $request->bill_phone;
$customer->bill_email = $request->bill_email;
$customer->save();
$salesorder = New SalesOrder;
$salesorder->pid = $request->pid;
$salesorder->project_name = $request->project_name;
$salesorder->customer_id = $request->company_id;
$salesorder->total = $request->totalfee;
$salesorder->status = 'submit';
$salesorder->detail = $request->detail;
$salesorder->save();
}else{
$salesorder = New SalesOrder;
$salesorder->pid = $request->pid;
$salesorder->project_name = $request->project_name;
$salesorder->customer_id = $request->company_id;
$salesorder->total = $request->totalfee;
$salesorder->status = 'submit';
$salesorder->detail = $request->detail;
$salesorder->save();
//dd($salesorder);
}
dd($request->all()); result
array:32 [▼
"sales_order_id" => "9"
"select_data" => "new"
"company_id" => "2"
"company_name" => "PT Bank ABC"
"address" => "JL Sudirman"
"tc_name" => "Budi"
"tc_dept" => "Technical"
"tc_phone" => "0812345678"
"tc_email" => "budi#abc.co.id"
"bill_name" => "Joko"
"bill_dept" => "Finance"
"bill_phone" => "08123456789"
"bill_email" => "joko#abc.co.id"
"pid" => "PID002"
"project_name" => "Implementasi"
"order_identifier" => array:2 [▶]
"service_name" => array:2 [▶]
"service_id" => array:2 [▶]
"order_type" => array:2 [▶]
"select_plan" => array:2 [▶]
"qty" => array:2 [▶]
"unit_price" => array:2 [▶]
"total_price" => array:2 [▶]
"note" => array:2 [▶]
"emp_name" => array:1 [▶]
"emp_id" => array:1 [▶]
"position" => array:1 [▶]
"position_id" => array:1 [▶]
"mandays" => array:1 [▶]
"detail" => "Coba Coba"
"totalfee" => "3100"
"_token" => "uxmXBwJKHWIoXDSFetU4oRgTiTftYEhhdpx4CaPr"
]
radio button html code
<div class="row-fluid select_data">
<input name="select_data" id="select_data" type="radio" value="new">
<span class="lbl">New</span>
<input name="select_data" id="select_data" type="radio" value="existing">
<span class="lbl">Existing</span>
</div>
The problem is because some of $request properties are arrays. You can't store an array in integer, string etc columns. If you want to store them as arrays, you can serialize them:
$customer->service_id = json_encode(service_id);
And store in text or json columns:
$table->json('service_id');
$table->text('service_id');
Try to change id of both the radio buttons, you can not set same id for different elements, it will behave differently
Try this one
<input name="select_data" name="select_data" type="radio" value="new">
<span class="lbl">New</span>
<input name="select_data" name="select_data" type="radio" value="existing">
<span class="lbl">Existing</span>