获取数组数据并插入数据库

时间:2018-01-09 14:34:52

标签: arrays codeigniter csv

我正在尝试获取来自另一个函数的数组中的数据(该函数正在提取csv文件中的数据),当我尝试从该数组调用两个字段时,它显示错误,它是< strong> 未识别的变量

函数action()中显示的$this->csv_process();是从csv文件中提取数据并将其存储在一个成功的数组中的函数,因为我尝试在var_dump();上检查它

我还将这两个字段命名为$ name和$ email,如下所示:

功能CSV_process()

public function csv_process()
    {
        /* variables for openning the csv file */                        

            if (!in_array($extension, $allowed_ext)) {
                    $this->session->set_flashdata("message", "Sorry, CSV file only.");            
            } else {
                if ($filesize > 0) {
                $file         = fopen($filename, "r");
                $toWrite     = array();        
                $error         = false;
                $col_size     = 2;
                $skip         = 0;                                
                    while ($data = fgetcsv($file, 10000, ","))
                    {
                        $skip++;
                        if ($skip == 1) {
                            continue;
                        }
                        $numofcol = count($data);
                        if ($numofcol != $col_size ) {
                            $this->session->set_flashdata("message", "Column count exceeded or missing.");
                        } else {
                            $name1         = $data[0];
                            $name         = str_replace("'", "''", $name1);
                            $email1     = $data[1];
                            $email         = str_replace("'", "''", $email1);
                            $toWrite[]    = [
                                        'name'  => $name,
                                        'email' => $email
                            ];    
                        }                        
                    }
                }
            }

            return $toWrite;
    }

功能动作()

function action(){
$toWrite[] = $this->csv_process();
    foreach ($toWrite as $arr) {
        list($name, $email) = $arr;
        //die(var_dump($arr));
        $query = $this->db->query("SELECT * FROM import WHERE name ='$name'  AND email = '$email'");
        if ($query->num_rows() >= 1) {

        } else {
            if ($name == "" OR $email == "") {
            } else {
                if ((filter_var($email, FILTER_VALIDATE_EMAIL)) == FALSE ) {
                } else {
                    $this->db->query("INSERT INTO import(name, email, created_date) VALUES('".$name."', '".$email."', '".date("Y-m-d h-i-s")."')");
                    $this->session->set_flashdata('message', 'SUCCESS YEAY');
                    redirect('Clean_csv/index');
                }
            }
        }
        $query->free_result();
    }
 }

列表数组似乎不适用于此处,任何人都知道如何从 $ arr中提取数据数组?

1 个答案:

答案 0 :(得分:0)

您无需提取值。您可以在bound query中使用每个$arr。它简化了select查询的语法。

插入使用CodeIgniter的insert()方法。同样,$arr可以通过在尝试插入之前向其添加date来直接使用。

我认为这会奏效。

function action()
    {
        $toWrite[] = $this->csv_process();
        foreach($toWrite as $arr)
        {
            $query = $this->db->query("SELECT * FROM import WHERE name=?  AND email=?", $arr);
            if($query->num_rows() >= 1)
            {}
            else
            {
                if($arr['name'] == "" OR $arr['email'] == "")
                {}
                else
                {
                    if((filter_var($email, FILTER_VALIDATE_EMAIL)) == FALSE)
                    {}
                    else
                    {
                        $arr['created_date'] = date("Y-m-d h-i-s");
                        $this->db->insert("import", $arr);
                        $this->session->set_flashdata('message', 'SUCCESS YEAY');

                        //??? redirect('Clean_csv/index');  
                        //Are you sure, you may still have more $arr in $toWrite to process - right?
                    }
                }
            }
            $query->free_result();
        }
    }

你需要知道在循环中重复运行数据库查询是多么可怕的想法。即使您使用free_result(),也可能会大量消耗服务器资源。如果您的csv文件有几千个项目,则会严重影响数据库和服务器。