How to insert multiple dropdown value in database? - codeigniter

I have many dropdown list in my html and I want to send each value from some dropdown in database (which dropdown I open and select any value that should go into database with the dropdown name as well).
Database will show like this that this user selected value 1 from "ABC Dropdown" and value 3 from "XYZ" dropdown and etc etc and those dropdown whose value I don't select should not go into database.
<pre>
<tr class="evenrow">
<td align="center"><input type="hidden" name="report_id[]" id="report_id" value="1">1</td>
<td>Customer Balances</td>
<td>
<select name="printer[]" id="printer" class="form-control" title="">
<option value="">Browser support</option>
<option value="1">QL500 - Label printer</option>
<option value="2">Samsung - Main network printer</option>
<option value="3">Local - Local print server at user IP</option>
</select>
</td>
</tr>
<tr class="evenrow">
<td align="center"><input type="hidden" name="report_id[]" id="report_id" value="2">2</td>
<td>Customer Balances</td>
<td>
<select name="printer[]" id="printer" class="form-control" title="">
<option value="">Browser support</option>
<option value="1">QL500 - Label printer</option>
<option value="2">Samsung - Main network printer</option>
<option value="3">Local - Local print server at user IP</option>
</select>
</td>
</tr>
<tr class="evenrow">
<td align="center"><input type="hidden" name="report_id[]" id="report_id" value="3">3</td>
<td>Customer Balances</td>
<td>
<select name="printer[]" id="printer" class="form-control" title="">
<option value="">Browser support</option>
<option value="1">QL500 - Label printer</option>
<option value="2">Samsung - Main network printer</option>
<option value="3">Local - Local print server at user IP</option>
</select>
</td>
</tr>
<tr class="evenrow">
<td align="center"><input type="hidden" name="report_id[]" id="report_id" value="4">4</td>
<td>Customer Balances</td>
<td>
<select name="printer[]" id="printer" class="form-control" title="">
<option value="">Browser support</option>
<option value="1">QL500 - Label printer</option>
<option value="2">Samsung - Main network printer</option>
<option value="3">Local - Local print server at user IP</option>
</select>
</td>
</tr>
</table>
</div>
<!-- /.box-body -->
</div>
<center>
<div class="input-group">
<input type="submit" name="submit" id="submit" value="Add New" class="btn btn-info pull-right">
</div>
</center>
</pre>

Why not using the different name instead of same at all.
Report1, Printer1
Report2, Printer2
Report3, Printer3
and in controller you can catch them by the name...
<table>
<tr class="evenrow">
<td align="center"><input type="hidden" name="report_id1" id="report_id" value="1">1</td>
<td>Customer Balances</td>
<td>
<select name="printer1" id="printer" class="form-control" title="">
<option value="">Browser Support</option>
<option value="1">Text</option>
<option value="2">Text</option>
<option value="3">Text</option>
</select>
</td>
</tr>
<tr class="evenrow">
<td align="center"><input type="hidden" name="report_id2" id="report_id" value="2">2</td>
<td>Customer Balances</td>
<td>
<select name="printer2" id="printer" class="form-control" title="">
<option value="">Browser Support</option>
<option value="1">Text</option>
<option value="2">Text</option>
<option value="3">Text</option>
</select>
</td>
</tr>
</table>
And then in controller :
$report1 = $this->input->post('report1');
$report2 = $this->input->post('report2');
$printer1= $this->input->post('printer1');
$printer2= $this->input->post('printer2');

you used array for select box name and all three select box have same name so if you want to store all different section values differ change select box name so you get the 2 different array in controller or you can easily identifier from which section its got and make a customer json
$selected_option= array("section1"=>$this->input->post('printer1',"section2"=>$this->input->post('printer2');
$selected_option_json= json_encode($selected_option);
store $selected_option_json(its a json string) in db
Hope its helpful for you.

Related

Livewire - Multiple root elements detected

I'm creating a table, whose rows are wrapped inside a livewire component
<div class="card">
<div class="card-body">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Nombre del Riesgo</th>
<th>Información</th>
<th>Dueño del Riesgo</th>
<th>Costo Adicional</th>
<th>Prevención</th>
</tr>
</thead>
<tbody>
<div>
#foreach($risks as $risk)
<livewire:risk-row :risk="$risk"/>
#endforeach
</div>
</tbody>
</table>
</div>
</div>
But in the row component, whenever I change an input, or select something from a dropdown select, the component is not re rendering.
I was wondering why this could be happening. So I opened my console and saw:
Livewire: Multiple root elements detected. This is not supported. See docs for more information https://laravel-livewire.com/docs/2.x/troubleshooting#root-element-issues <div wire:id=​"6CNMP1hiBR3Qy7IbmfbQ">​ ​</div>​
value # index.js:85
Component # index.js:27
(anonymous) # index.js:88
value # index.js:87
(anonymous) # schedule:432
Here's the component blade file
<div>
<tr>
<td>{{ $risk['name'] }}</td>
<td>
<div><strong>Proceso: </strong>{{ $risk['process']['name'] }}</div>
<div><strong>Frecuencia: <span class="badge badge-secondary text-sm">{{ $risk['frequency_label'] }}</span></strong></div>
<div><strong>Impacto: </strong><span class="badge badge-info text-sm">{{ $risk['impact_label'] }}</span></div>
<div><strong>Riesgo: </strong><span class="badge badge-{{ $risk['risk_color'] }} text-sm">{{ $risk['risk_label'] }}</span></div>
</td>
<td>
<select name="owner" id="owner" class="form-control" wire:change="test">
#foreach(App\Models\Risk::OWNERS as $owner)
<option value="{{ $owner['id'] }}">{{ $owner['name'] }}</option>
#endforeach
</select>
{{ $owner_id }}
</td>
<td>
<select name="owner" id="owner" class="form-control">
#foreach(App\Models\Risk::COSTS as $cost)
<option value="{{ $cost['id'] }}">{{ $cost['name'] }}</option>
#endforeach
</select>
</td>
<td>
<select name="owner" id="owner" class="form-control">
#foreach(App\Models\Risk::PREVENTIONS as $prevention)
<option value="{{ $prevention['id'] }}">{{ $prevention['name'] }}</option>
#endforeach
</select>
</td>
</tr>
</div>
Is there any workaround for this?
When using livewire inside a foreach, you need to add a key.
<tbody>
#foreach ($risks as $risk)
<livewire:risk-row :risk="$risk" :wire:key="$loop->index">
#endforeach
</tbody>
Also, you could use the <tr> as the root element and avoid having <div> inside the <tbody>.
Finally, you're using a lot of id and name attributes that repeat over and over. You should not have duplicate ids. As for the name attributes, you can use the array notation.
<tr>
<td>{{ $risk['name'] }}</td>
<td>
<div><strong>Proceso: </strong>{{ $risk['process']['name'] }}</div>
<div><strong>Frecuencia: <span class="badge badge-secondary text-sm">{{ $risk['frequency_label'] }}</span></strong></div>
<div><strong>Impacto: </strong><span class="badge badge-info text-sm">{{ $risk['impact_label'] }}</span></div>
<div><strong>Riesgo: </strong><span class="badge badge-{{ $risk['risk_color'] }} text-sm">{{ $risk['risk_label'] }}</span></div>
</td>
<td>
<select name="owner[]" class="form-control" wire:change="test">
#foreach(App\Models\Risk::OWNERS as $owner)
<option value="{{ $owner['id'] }}">{{ $owner['name'] }}</option>
#endforeach
</select>
{{ $owner_id }}
</td>
<td>
<select name="cost[]" class="form-control">
#foreach(App\Models\Risk::COSTS as $cost)
<option value="{{ $cost['id'] }}">{{ $cost['name'] }}</option>
#endforeach
</select>
</td>
<td>
<select name="prevention[]" class="form-control">
#foreach(App\Models\Risk::PREVENTIONS as $prevention)
<option value="{{ $prevention['id'] }}">{{ $prevention['name'] }}</option>
#endforeach
</select>
</td>
</tr>

How to use old() helper in input fields of Laravel blade

In my Laravel-5.8 project I am working on a leave application that will iterate with employee_type. The fields in the view blade are array elements. The code is as shown below:
#foreach ($employeetypes as $key=> $employeetype)
<tr>
<td width="5%">
{{$key+1}}
</td>
<td width="30%">
<span>{{$employeetype->employee_type_name}}</span>
</td>
<td width="20%"><input type="number" name="no_of_days[]" placeholder="Enter leave days here" class="form-control no_of_days" max="120">
</td>
<td width="15%">
<select class="form-control select2bs4" data-placeholder="Select Applicable Gender" tabindex="1" name="leave_applicable_gender[]">
<option value="0" selected="true" disabled="true">Select Applicable Gender</option>
#foreach(AppHelper::LEAVE_APPLICABLE_GENDER as $key1 => $value)
<option value="{{ $key1 }}" {{ (($applicableGender == $key1) || (is_null($applicableGender) && $key1 == 1)) ? 'selected' : '' }}>
{{ $value }}
</option>
#endforeach
</select>
</td>
<td width="10%"><input type="checkbox" name="weekend_inclusive{{$key}}" class="form-control" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES">
</td>
#endforeach
Everything submits to the database successfully.
Since all are array fields, how do I add old() helper to each of the input fields (number, dropdown and checkbox)?
Thank you

How to get Selected checkbox into database

I have a page for purchase order in that page i show data from table obat and i want to get selected checkbox using that data into table purchase order
This is my table obat
<table class="table table-bordered" style="margin-top:20px">
<tbody>
<tr class="text-center">
<th>No</th>
<td>Kode Obat</td>
<td>Nama Obat</td>
<td>Harga</td>
</tr>
#foreach ($obat as $key =>$o)
<tr>
<th class="text-center">
#foreach ($po as $po)
<input type="checkbox" name="select" id="select">
#endforeach
</th>
<td>
<input type="text" class="form-control" value="{{ $o->kode_obat }}">
</td>
<td>
<input type="text" class="form-control" value="{{ $o->nama_obat }}">
</td>
<td>
<input type="text" class="form-control" value="{{ $o->harga_obat }}">
</td>
</tr>
#endforeach
</tbody>
</table>
The checkbox(select) is from table purchase order but it can't show. if i did't use foreach its show
you can pass array also to check if it is in the array or not then you can this like below.
in your controller
public function edit($id)
{
$odat=Odat::all();
foreach($odat as $od)
{
$listod[]=$odat->select;
}
return view('yourbladefile',compact('roles','listod'));
}
then in your view
<input type="checkbox" name="select" #if (in_array(yourpurchaseordernamefromdb, $listod))
{{'checked'}} #endif>Selected</td>
personally i managed liked this. hope it helps

Model::updateOrCreate actually updates but does not create when record does not exist

I am building an app where if a user wants to update any records the user can edit and update but also add more fields on the front-end so that does data entered in the fields can be added to the records and if user edited anything, that too can updated change successfully.
So there are tests(question) that a user is supposed to do and each test has options just like an examination.
When I try to change anything and click on the update button the record gets updated successfully but if I add a new input as part of the form, it only updates the record and does not add the new one.
This is my HTML:
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Contraceptive pill</th>
<th>UID</th>
<th>Implant</th>
<th>Injectable</th>
<th>Male condom</th>
<th>Female condom</th>
<th>Fertility awareness</th>
<th>Withdrawal</th>
<th>Sterilization</th>
<th>Emergency contraceptive</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="test-inputs">
#foreach($Test0ptions as $option)
<tr>
<td></td>
<td>
<input class="form-control" type="number" name="contraceptive_pills[]" required value="{{ $option->contraceptive_pill }}">
</td>
<td>
<input class="form-control" type="number" name="uids[]" required value="{{ $option->uid}}">
</td>
<td>
<input class="form-control" type="number" name="implants[]" required value="{{ $option->implant}}">
</td>
<td>
<input class="form-control" type="number" name="injectables[]" required value="{{ $option->injectable}}">
</td >
<td>
<input class="form-control" type="number" name="male_condoms[]" required value="{{ $option->male_condom}}">
</td>
<td>
<input class="form-control" type="number" name="female_condoms[]" required value="{{ $option->female_condom}}">
</td>
<td>
<input class="form-control" type="number" name="fertility_awareness[]" required value="{{ $option->fertility_awareness}}">
</td>
<td>
<input class="form-control" type="number" name="withdrawals[]" required value="{{ $option->withdrawal}}">
</td>
<td>
<input class="form-control" type="number" name="sterilizations[]" required value="{{ $option->sterilization}}">
</td>
<td>
<input class="form-control" type="number" name="emergency_contraceptives[]" required value="{{ $option->emergency_contraceptive}}">
</td>
</tr>
</tbody>
</table>
<div class="form-group" style="padding-bottom: 10px;">
<button type="submit" class="btn primary"><i class="fa fa-save"></i> Update</button>
</div>
This is my script that will add new set of form:
populateNewTestForms() {
$('#test-inputs').append(`
<tr>
<td>${this.incrementTableCounter+=1}</td>
<td><input class="form-control" type="number" name="contraceptive_pills[]" required> </td>
<td> <input class="form-control" type="number" name="uids[]" required> </td>
<td> <input class="form-control" type="number" name="implants[]" required> </td>
<td> <input class="form-control" type="number" name="injectables[]" required> </td>
<td> <input class="form-control" type="number" name="male_condoms[]" required> </td>
<td> <input class="form-control" type="number" name="female_condoms[]" required> </td>
<td> <input class="form-control" type="number" name="fertility_awareness[]" required> </td>
<td> <input class="form-control" type="number" name="withdrawals[]" required> </td>
<td> <input class="form-control" type="number" name="sterilizations[]" required> </td>
<td> <input class="form-control" type="number" name="emergency_contraceptives[]" required>
</td>
<td>
<button type="button" onclick=" $(this).closest('tr').remove(); " class="btn btn-danger" title="delete"><i class="fa fa-trash"></i></button>
</td>
</tr>
`);
This is my controller :
foreach ($request->option_ids as $option_id) {
TestOption::updateOrCreate(['id' => $option_id], [
'test_id' => $test->id,
'name' => $request->names[$i],
'contraceptive_pill' => $request->contraceptive_pills[$i],
'uid' => $request->uids[$i],
'implant' => $request->implants[$i],
'injectable' => $request->injectables[$i],
'male_condom' => $request->male_condoms[$i],
'female_condom' => $request->female_condoms[$i],
'fertility_awareness' => $request->fertility_awareness[$i],
'withdrawal' => $request->withdrawals[$i],
'sterilization' => $request->sterilizations[$i],
'emergency_contraceptive' => $request->emergency_contraceptives[$i]
]);
Your function searches for option only, try swapping them around like this.
TestOption::updateOrCreate([
'test_id' => $test->id
'name' => $request->names[$i],
'contraceptive_pill' => $request->contraceptive_pills[$i],
'uid' => $request->uids[$i],
'implant' => $request->implants[$i],
'injectable' => $request->injectables[$i],
'male_condom' => $request->male_condoms[$i],
'female_condom' => $request->female_condoms[$i],
'fertility_awareness' => $request->fertility_awareness[$i],
'withdrawal' => $request->withdrawals[$i],
'sterilization' => $request->sterilizations[$i],
'emergency_contraceptive' => $request->emergency_contraceptives[$i]
],['id' => $option_id]);

How to call a variable linked to an option value within a JSP page

I have a few option values within my JSP page. I want to trigger a query based on what option value is chosen. Basically if the option value selected is either 33 or 36, execute one query, else execute the other query. Please see sample code below:
<table border="0" align='center'>
<tr>
<td>
<h2>Division</h2>
</td>
<td align='left'>
<jsp:useBean id="masterDaoUtil" class="master.dao.util.MasterDataConstants"/>
<select name="divisionId" id="divisionId">
<option value="33"><%=MasterDataConstants.DIVISION_TYPE_AUDIT_MANAGEMENT_GLOBAL_NAME%></option>
<option value="31"><%=MasterDataConstants.DIVISION_TYPE_CHANGE_MANAGEMENT_GLOBAL_NAME%></option>
<option value="34"><%=MasterDataConstants.DIVISION_TYPE_DEA_MANAGEMENT_GLOBAL_NAME%></option>
<option value="35"><%=MasterDataConstants.DIVISION_TYPE_EHS_MANAGEMENT_GLOBAL_NAME%></option>
<option value="23"><%=MasterDataConstants.DIVISION_TYPE_EVENT_MANAGEMENT_GLOBAL_NAME%></option>
<option value="36"><%=MasterDataConstants.DIVISION_TYPE_QUALITY_EVENT_MANAGEMENT_GLOBAL_NAME%></option>
<option value="40"><%=MasterDataConstants.DIVISION_TYPE_NORAMCO_AUDIT_MANAGEMENT_GLOBAL_NAME%></option>
<option value="43"><%=MasterDataConstants.DIVISION_TYPE_NORAMCO_CHANGE_MANAGEMENT_GLOBAL_NAME%></option>
<option value="41"><%=MasterDataConstants.DIVISION_TYPE_NORAMCO_DEA_MANAGEMENT_GLOBAL_NAME%></option>
<option value="42"><%=MasterDataConstants.DIVISION_TYPE_NORAMCO_EHS_MANAGEMENT_GLOBAL_NAME%></option>
<option value="44"><%=MasterDataConstants.DIVISION_TYPE_NORAMCO_EVENT_MANAGEMENT_GLOBAL_NAME%></option>
</select></td>
</tr>
<tr>
<td>
<h2>Site Name</h2>
</td>
<td align='left'>
<jsp:useBean id="masterDao" class="master.dao.MasterDataDao"/>
<select name="siteId" id="siteId">
<option value="0">ALL</option>
****<%if (divisionId == 33 | divisionId == 36) {%>
<c:forEach items="${masterDao.allJJSites}" var="siteDto">
<option value="${siteDto.id}">${siteDto.name}</option>
</c:forEach>
<%} else { %>
<c:forEach items="${masterDao.allSites}" var="siteDto">
<option value="${siteDto.id}">${siteDto.name}</option>
</c:forEach>
<% } %>****
</select></td>
</tr>
</table>
I have tried to make my if-else clause bold but not sure if it is showing. Basically it is located where the **** is. Obviously I cannot use divisionId as a variable but I wanted to know how I can either set a variable to the option value selected or see if there was another way to do this.

Resources