[英]Codeigniter Array to Database with foreach
I'm trying to store details about uploaded files in the DB. 我正在尝试将有关上传文件的详细信息存储在数据库中。
Presently details of single uploaded files go into an array. 目前,单个上传文件的详细信息进入一个数组。 I have no idea on how I should write this info into the DB.
我不知道如何将这些信息写入数据库。
I think this must be done using a foreach loop with the array but I'm not sure how to do this. 我认为必须使用数组的foreach循环来完成此操作,但是我不确定如何执行此操作。
Please advise. 请指教。
My model looks like this: 我的模型如下所示:
class download_model extends CI_Model {
//
// Add file
public function add() {
$new_file = array(
'file_author_id' => $this->profil_model->get_user_id(),
'file_author_name' => $this->profil_model->get_firstname().' '.$this->profil_model->get_lastname(),
'file_name' => $this->input->post('file_name'), <-- IN THIS I MUST RUN LOOP TO SEND MULTIPLE VALUES- I HAVE NO IDEA HOW I CAN DO THIS :(
);
$this->db->insert('download', $new_file);
}
}
This is my upload file(s) result: 这是我的上传文件结果:
Array
(
[pageTitle] => Multiple file upload
[uploads] => Array
(
[0] => Array
(
[file_name] => MY_FILE_NAME.txt
[file_type] => text/plain
[file_path] => DIR
[full_path] => DIR/MY_FILE_NAME.txt
[raw_name] => MY_FILE_NAME
[orig_name] => MY_FILE_NAME.txt
[client_name] => MY_FILE_NAME.txt
[file_ext] => .txt
[file_size] => 0.98
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
[1] => Array
(
[file_name] => MY_FILE_NAME_2.txt
[file_type] => text/plain
[file_path] => DIR
[full_path] => MY_FILE_NAME_2.txt
[raw_name] => MY_FILE_NAME_2
[orig_name] => MY_FILE_NAME_2.txt
[client_name] => MY_FILE_NAME_2.txt
[file_ext] => .txt
[file_size] => 0.98
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
)
)
Please, help me or do anything how I can do this :( It's possible to do :( ? 请帮助我或做任何我该如何做的事情:(可以做:(?
you should change your code a bit. 您应该稍微更改一下代码。
You model should look like this: 您的模型应如下所示:
public function add($userId, $authorName, $fileName) {
$new_file = array(
'file_author_id' => $userId,
'file_author_name' => $authorName,
'file_name' => $fileName,
);
return $this->db->insert('download', $new_file);
}
And in your Controller you should do the needed foreach like this: 在您的控制器中,您应该执行以下所需的操作:
public function upload() {
//GETTING YOUR DATA RIGHT HERE
$userId = $this->profil_model->get_user_id();
$authorName = $this->profil_model->get_firstname().' '.$this->profil_model->get_lastname();
foreach ($uploads as $upload) {
$this->model_name->add($userId, $authorName, $upload['file_name']);
}
}
Or if you want 1 row with all uploaded files in that field, do this: 或者,如果您希望在该字段中包含所有已上传文件的1行,请执行以下操作:
public function upload() {
//GETTING YOUR DATA RIGHT HERE
$userId = $this->profil_model->get_user_id();
$authorName = $this->profil_model->get_firstname().' '.$this->profil_model->get_lastname();
$fileString = '';
$countUploads = count($uploads);
$i = 1;
foreach ($uploads as $upload) {
$fileString .= $upload['file_name']
if ($i < $countUploads) {
$fileString .= ',';
}
$i++;
}
$this->model_name->add($userId, $authorName, $fileString);
}
A very much simpler approach can be applied using insert_batch function 使用insert_batch函数可以应用非常简单的方法
<?php
public function add() {
//prepare the array of data
$insert = array_map(function($row){
return array(
'file_author_id' => $this->profil_model->get_user_id(),
'file_author_name' => $this->profil_model->get_firstname().' '.$this->profil_model->get_lastname(),
'file_name' => $row['file_name']
);
},$this->input->post('uploads'));
$this->db->insert_batch('download', $insert);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.