I am just new in CodeIgniter and still learning and exploring it. I have this view where the user adds category and sizes. It has the unlimited or dynamic input field in sizes, so if the category for ex. Pants, pants have many sizes so the user will just click the add more button. But here's the thing I am stocked in saving all the inputs. I want to save the category and the corresponding size of the category. Here's my table
CREATE TABLE `category` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`category_name` varchar(55) NOT NULL,
PRIMARY KEY (`category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;
CREATE TABLE `sizes` (
`size_id` int(11) NOT NULL AUTO_INCREMENT,
`size` varchar(45) NOT NULL,
`category_id` int(11) NOT NULL,
PRIMARY KEY (`size_id`),
KEY `cat_fk_idx` (`category_id`),
CONSTRAINT `cat_fk` FOREIGN KEY (`category_id`) REFERENCES `category`
(`category_id`)
)
And here's my view.php
<div class="col-sm-5">
<?php echo $this->session->flashdata('errorMessage');?>
<?php echo $this->session->flashdata('successMessage');?>
<?php echo form_open('category_con/add_category'); ?>
<?php echo form_fieldset('<h1 class="text-danger">Categories</h1>'); ?>
<div class="form-group">
<label for='Username'>Add New Category</label>
<input type="text" name="category_name" class="form-control" placeholder="Add new Category here">
</div>
<script src="assets/js/jquery.min.js"></script>
<div class="form-group">
<form name="add_name" id="add_name">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td><input type="text" name="name[]" placeholder="Add Size" class="form-control name_list" /></td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table>
</div>
</form>
</div>
<div class="form-group">
<input type="submit" name="save_category" class="btn btn-success" value="Save Category">
</div>
</div>
<script>
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<tr id="row'+i+'"><td><input type="text" name="name[]" placeholder="Add Size" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
$('#submit').click(function(){
$.ajax({
url:"name.php",
method:"POST",
data:$('#add_name').serialize(),
success:function(data)
{
alert(data);
$('#add_name')[0].reset();
}
});
});
});
</script>
I hope you would help me. Thanks.
答案 0 :(得分:1)
Create a model with name Test.php
1
You can pass as much as data dynamically into model class
In somewhere on your controller
<?php
class Test extends CI_Model
{
public function create()
{
$data = array(
array(
'category_name' => 'Data Input of category_name' // data input (category_name)
),
array(
'category_name' => 'Data Input category_name' // data input (category_name)
)
);
$this->db->insert_batch('category', $data);
$last_row_id = $this->db->insert_id(); // Gets last row id ( category_id )
$data = array(
array(
'size' => 'Data input of size' ,
'category_id' => $last_row_id
),
array(
'size' => 'Data input of size' ,
'category_id' => $last_row_id
)
);
$this->db->insert_batch('sizes', $data);
}
}