无法根据用户选择的输入数量来使用正确的键保存数组。
我尝试了以下方法:
我尝试对一些元素进行json编码,但无济于事。
这是html:
<div class="row">
<div class="col">
<div class="form-group">
<label>Block Name</label>
<input type="text" name="name_of_blocks[]" class="form-control">
</div>
</div>
<div class="col">
<label>No of units in the block</label>
<input type="number" name="no_units[]" class="form-control" id="blockNo">
</div>
<div class="col">
<label>Number of floors</label>
<input type="number" class="form-control" name="no_of_floors[]">
</div>
<div class="col">
<label>Does this block have a lift?</label>
<select class="form-control" name="lift[]">
<option>Please Select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
</div>
<div class="row">
<div class="col">
<label>What facilities does this block have?</label>
<div class="row">
<div class="col">
<ul class="list-unstyled">
<li><input type="checkbox" name="facilities[]" value="fob"> Fob Entry</li>
<li><input type="checkbox" name="facilities[]" value="gate_entry"> Gate Entry</li>
<li><input type="checkbox" name="facilities[]" value="key"> Key Entry</li>
<li><input type="checkbox" name="facilities[]" value="finger"> Fingerprint Entry</li>
<li><input type="checkbox" name="facilities[]" value="Code Entry"> Code Entry</li>
<li><input type="checkbox" name="facilities[]" value="mailbox"> Mailbox Area</li>
</ul>
</div>
<div class="col">
<ul class="list-unstyled">
<li><input type="checkbox" name="facilities[]" value="bar"> Bar</li>
<li><input type="checkbox" name="facilities[]" value="bike_storage"> Bike Storage</li>
<li><input type="checkbox" name="facilities[]" value="car_park"> Secure Car Parking</li>
<li><input type="checkbox" name="facilities[]" value="off_road"> Off-road Parking</li>
<li><input type="checkbox" name="facilities[]" value="visitor_parking"> Visitor Parking</li>
<li><input type="checkbox" name="facilities[]" value="concierge"> Concierge</li>
</ul>
</div>
<div class="col">
<ul class="list-unstyled">
<li><input type="checkbox" name="facilities[]" value="gym"> Gym</li>
<li><input type="checkbox" name="facilities[]" value="pool"> Swimming Pool </li>
<li><input type="checkbox" name="facilities[]" value="snooker"> Snooker/Pool Table</li>
<li><input type="checkbox" name="facilities[]" value="cafe"> Cafe</li>
<li><input type="checkbox" name="facilities[]" value="maintenance"> Maintenance Service</li>
<li><input type="checkbox" name="facilities[]" value="various"> Various businesses/commercial outlets</li>
</ul>
</div>
这是我控制器中的所有内容,但这要么无法正确保存,要么如果有两行又不保存正确的信息就保存一行:
$data = $request->all();
$count = $data['number_of_blocks'];
for ($i = 0; $i < $count; $i++) {
$fac = array_values($request->facilities);
$facil = json_encode($fac);
$blok = new Blocks;
$blok->block_id = $block->id;
$blok->name_of_blocks = array_key_exists($i, $data['name_of_blocks']) ? $data['name_of_blocks'][$i] : null;
$blok->no_units = array_key_exists($i, $data['no_units']) ? $data['no_units'][$i] : null;
$blok->no_of_floors = array_key_exists($i, $data['no_of_floors']) ? $data['no_of_floors'][$i] : null;
$blok->lift = array_key_exists($i, $data['lift']) ? $data['lift'][$i] : null;
$blok->facilities = array_key_exists($i, $facil) ? $facil[$i] : null;
$blok->save();
}
我确实有一些克隆脚本,可以根据用户想要的数量来克隆表单域的数量,所以这也可能是一个问题,但是我不确定是什么地方或什么地方:
$("#continue").click(function () {
$("#first").hide();
$("#second").show();
var blocks = $("#noBlocks").val();
var name = $("#blockName").val();
$(".clone").hide();
var select = parseInt(blocks, 10);
var $clone = $('.clone').html();
var html = '';
while (select > 0) {
html += $clone;
select--;
}
$("#area").empty().html(html);
$("#bno").html(blocks);
$("#bn").html(name);
});
基本上,如果用户选择2,那么我希望代码保存两行,并且为每行保存json编码的功能,并匹配用户选择的内容。任何帮助建议都将不胜感激。