I’ve form to insert data use insert_batch CI 3, my plan is use insert_batch to insert more data in one time and javascript to create dynamically the form. Below the code I’ve to try it.
Controller
public function insert_data()
{
// Processing uploaded images
$config['upload_path'] = 'training_assets/image_training/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024'; // Maximum size allowed in KB
$this->load->library('upload', $config);
// Get the text field values
$id_article = $this->input->post("id_article");
$date = $this->input->post('date', TRUE);
// Initialize an array to hold the batch data
$data = array();
foreach ($_FILES['images']['name'] as $key => $image) {
$_FILES['userfile']['name'] = $_FILES['images']['name'][$key];
$_FILES['userfile']['type'] = $_FILES['images']['type'][$key];
$_FILES['userfile']['tmp_name'] = $_FILES['images']['tmp_name'][$key];
$_FILES['userfile']['error'] = $_FILES['images']['error'][$key];
$_FILES['userfile']['size'] = $_FILES['images']['size'][$key];
// Check if file upload is successful
if ($this->upload->do_upload()) {
$upload_data = $this->upload->data();
// Prepare row data for insertion
$row_data = array(
'question' => $this->input->post('question', TRUE)[$key],
'a' => $this->input->post('a', TRUE)[$key],
'b' => $this->input->post('b', TRUE)[$key],
'c' => $this->input->post('c', TRUE)[$key],
'd' => $this->input->post('d', TRUE)[$key],
'key' => $this->input->post('key', TRUE)[$key],
'img' => $upload_data['file_name']
);
// Add non-array data to each row
$row_data['id_article'] = $id_article;
$row_data['date'] = $date;
// Add row data to the batch
$data[] = $row_data;
} else {
echo "File upload failed for image $key.";
}
}
// Insert data to database
if (!empty($data)) {
var_dump($data);
//$this->db->insert_batch('tb_question', $data);
} else {
echo "No data to insert.";
}
//redirect("question");
}
HTML
...<label>Question</label>
<textarea type="text" class="form-control" name="question[]" placeholder="Enter question" />
<input readonly id="id_article" class="form-control" name="id_article" />
<div class="col-sm-2">
<input readonly type="text" class="form-control" name="date" value="<?= date("Y/m/d"); ?>" />
</div>...
I want to insert id_article and date but not insert into array, how to do it?, and is it possible?,
thank you