簡體   English   中英

使用foreach的Codeigniter陣列到數據庫

[英]Codeigniter Array to Database with foreach

我正在嘗試將有關上傳文件的詳細信息存儲在數據庫中。

目前,單個上傳文件的詳細信息進入一個數組。 我不知道如何將這些信息寫入數據庫。

我認為必須使用數組的foreach循環來完成此操作,但是我不確定如何執行此操作。

請指教。

我的模型如下所示:

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);
    }
}

這是我的上傳文件結果:

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] => 
                )

        )

)

請幫助我或做任何我該如何做的事情:(可以做:(?

您應該稍微更改一下代碼。

您的模型應如下所示:

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);
}

在您的控制器中,您應該執行以下所需的操作:

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']);
    }
}

或者,如果您希望在該字段中包含所有已上傳文件的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);
}

使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM