In my application there is a form which have a two select field , in which the last select is set to have a multiple selection. What I need is to insert the first pair of select field to be inserted as a single record into the database. When I try to do this using a for loop I am able to insert the record as a single row until and unless I select only one value for my 2nd select field. But If I select multiple fields for second select, the records isn't proper.
Also here user can add more divs by clicking ADD button
<div id="scope-brand-div">
<div class="col-md-6">
<div class="form-group">
<label for="scope">Project Scope</label>
<select name="scope[]" class="form-control scope-select">
<option value=""></option>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="brands_used">Brands Used</label>
<select multiple name="brands_used[]" class="form-control brand-select">
<option value=""></option>
<option>D</option>
<option>E</option>
<option>F</option>
</select>
</div>
</div>
</div>
php
$s = $this->input->post('scope');
$b = $this->input->post('brands_used');
foreach( $s as $key => $n ) {
$rows[] = $n.",".$b[$key];
}
print_r($rows);
SCENARIO 1
If I select single fields for both select, the output will be:
A, D
B, E
SCENARIO 2
If I select a multiple filed for the second select like A,(D,E) and B,F. Using the same loop above the result will be:
A, D
B, E
How can I insert the same fields If I select multiple fields for 2nd select field ad provided the user can add more divs same like this.
Edit 1
<div id="scope-brand-div">
<div class="col-md-6">
<div class="form-group">
<label for="scope"><?php echo $this->lang->line('xin_project_scope');?></label>
<select name="scope[]" class="form-control select-border-color border-warning scope-select" data-plugin="select_hrm" data-placeholder="<?php echo $this->lang->line('xin_project_scope');?>">
<option value=""></option>
<?php foreach($all_project_scope as $scope) { ?>
<option value="<?php echo $scope->scope_name; ?>"><?php echo $scope->scope_name; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group brands-ajax">
<label for="brands_used"><?php echo $this->lang->line('xin_brands_used');?></label>
<select multiple name="brands_used[]" class="form-control select-border-color border-warning brand-select" data-plugin="select_hrm" data-placeholder="<?php echo $this->lang->line('xin_brands_used');?>">
<option value=""></option>
<?php foreach($all_brands as $brands) { ?>
<option value="<?php echo $brands->brand_name; ?>"><?php echo $brands->brand_name; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#add-new').click(function(){
var invoice_items = '<div id="scope-brand-div">'
+ '<div class="col-md-6">'
+'<div class="form-group">'
+ '<label for="scope"><?php echo $this->lang->line('xin_project_scope');?></label>'
+'<select name="scope[]" class="form-control scope-select" data-placeholder="<?php echo $this->lang->line('xin_project_scope');?>">'
+'<option value=""></option>'
<?php foreach($all_project_scope as $scope) { ?>
+'<option value="<?php echo $scope->scope_name; ?>"><?php echo $scope->scope_name; ?></option>'
<?php } ?>
+ '</select>'
+ '</div>'
+ '</div>'
+ '<div class="col-md-6">'
+ '<div class="form-group brands-ajax">'
+ '<label for="brands_used"><?php echo $this->lang->line('xin_brands_used');?></label>'
+'<select name="brands_used[]" class="form-control brand-select" data-placeholder="<?php echo $this->lang->line('xin_brands_used');?>">'
+'<option value=""></option>'
<?php foreach($all_brands as $brands) { ?>
+'<option value="<?php echo $brands->brand_name; ?>"><?php echo $brands->brand_name; ?></option>'
<?php } ?>
+'</select>'
+'</div>'
+ '</div>'
+'</div>'
$('#clone-parent').append(invoice_items).fadeIn(500);
});
});
</script>
<?php
public function add_project() {
$s = $this->input->post('scope');
$b = $this->input->post('brands_used');
foreach( $s as $key => $n ) {
$rows[] = $n.",".$b[$key];
//insert query
}
}
?>
Edit 2
echo var_export($this->input->post('brands_used'));
array (
0 =>
array (
0 => 'Opterna',
),
1 =>
array (
0 => 'Norden',
),
2 => 'Opterna',
)
echo var_export($this->input->post('scope'));
array (
0 => 'ACS',
1 => 'SMA TV',
)