public function save() {
$this->load->model('Users_model');
$this->load->model('Price_model');
$this->form_validation->set_rules('fname', 'fname', 'required');
$this->form_validation->set_rules('lname', 'lname', 'required');
$this->form_validation->set_rules('price', 'price', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('status', '< div class="alert alert-danger">< span>< b>ผิดพลาด b>"กรุณากรอกอีกครั้ง" span> div>');
redirect('Welcome','refresh');
}
else {
$array_table1['fname'] = $this->input->post('fname');
$array_table1['lname'] = $this->input->post('lname');
$array_table2['price'] = $this->input->post('price');
$insert_id_table1 = $this->Users_model->insert($array_table1);
$array_table2['user_id'] = $insert_id_table1;
$insert_id_table2 = $this->Price_model->insert($array_table2);
$array['price_id'] = $insert_id_table2;
$this->Users_model->update($insert_id_table1, $array);
$this->session->set_flashdata('status', '< div class="alert alert-success">< span>< b>สำเร็จ b>"บันทึกเรียบร้อย" span> div>');
redirect('Welcome','refresh');
}
}
public function insert($array) {
$this->db->insert('users', $array);
return $this->db->insert_id();
}
public function update($id, $array) {
$this->db->where('id', $id);
return $this->db->update('users', $array);
}
public function insert($array) {
$this->db->insert('price', $array);
return $this->db->insert_id();
}
< form action="< ?php echo site_url('Welcome/save'); ?>" method="post">
< div class="form-group">
< label for="fname">FirstName< /label>
< input type="text" class="form-control" id="fname" name="fname" placeholder="first name">
< /div>
< div class="form-group">
< label for="lname">LastName< /label>
< input type="text" class="form-control" id="lname" name="lname" placeholder="last name">
< /div>
< div class="form-group">
< label for="price">Price< /label>
< input type="text" class="form-control number-only" id="price" name="price" placeholder="Price">
< /div>
< div class="form-group row">
< div class="col-sm-10">
< button type="submit" class="btn btn-primary">Save< /button>
< /div>
< /div>
< /form>
public function getusers() {
$this->load->model('Users_model');
$data['data'] = $this->Users_model->get_users();
echo json_encode($data);
}
public function get_users() {
$this->db->from('users');
$query = $this->db->get();
return $query->result_array();
}
< div class="table-responsive">
< table id="table1" class="table table-striped table-hover table-bordered" style="width:100%">
< thead>
< tr>
< th>id< /th>
< th>price_id< /th>
< th>fname< /th>
< th>lname< /th>
< th>timestamp< /th>
< /tr>
< /thead>
< /table>
< /div>
< script>
$(document).ready(function() {
$('#table1').DataTable( {
"ajax": '< ?php echo site_url('Welcome/getusers'); ?>',
"columns": [
{ "data": "id" },
{ "data": "price_id" },
{ "data": "fname" },
{ "data": "lname" },
{ "data": "timestamp" },
]
} );
} );
< /script>
| id | price_id | fname | lname | timestamp |
|---|
public function getprice() {
$this->load->model('Price_model');
$data['data'] = $this->Price_model->get_price();
echo json_encode($data);
}
public function get_price() {
$this->db->from('price');
$query = $this->db->get();
return $query->result_array();
}
< div class="table-responsive">
< table id="table2" class="table table-striped table-hover table-bordered" style="width:100%">
< thead>
< tr>
< th>id< /th>
< th>user_id< /th>
< th>price< /th>
< th>timestamp< /th>
< /tr>
< /thead>
< /table>
< /div>
< script>
$(document).ready(function() {
$('#table2').DataTable( {
"ajax": '< ?php echo site_url('Welcome/getprice'); ?>',
"columns": [
{ "data": "id" },
{ "data": "user_id" },
{ "data": "price" },
{ "data": "timestamp" },
]
} );
} );
< /script>
| id | user_id | price | timestamp |
|---|
public function getusersandprice() {
$this->load->model('Users_model');
$data['data'] = $this->Users_model->tablejoin();
echo json_encode($data);
}
public function tablejoin() {
$this->db->select('*');
$this->db->from('users');
$this->db->join('price', 'users.id = price.user_id');
$query = $this->db->get();
return $query->result_array();
}
< div class="table-responsive">
< table id="table3" class="table table-striped table-hover table-bordered" style="width:100%">
< thead>
< tr>
< th>fname< /th>
< th>lname< /th>
< th>price< /th>
< th>timestamp< /th>
< /tr>
< /thead>
< /table>
< /div>
< script>
$(document).ready(function() {
$('#table3').DataTable( {
"ajax": '< ?php echo site_url('Welcome/getusersandprice'); ?>',
"columns": [
{ "data": "fname" },
{ "data": "lname" },
{ "data": "price" },
{ "data": "timestamp" },
]
} );
} );
< /script>
| fname | lname | price | timestamp |
|---|