简体   繁体   English

Codeigniter传递数据以查看并返回到控制器

[英]Codeigniter pass data to view and back to controller

I have a working CSV upload that takes the data pops it into the database and then opens a view with a table of the data for the user to review. 我有一个有效的CSV上传文件,它将数据弹出到数据库中,然后打开一个包含数据表的视图供用户查看。

What I would like to do is have a button on the view that then starts the processing of the data, and only process the new data that was added. 我想做的是在视图上有一个按钮,然后开始处理数据,并且仅处理添加的新数据。 If the table has 100 old rows but I add 50 new ones on upload I only want to process them. 如果表中有100条旧行,但是我在上传时添加了50条新行,那么我只想处理它们。 Sounds simple but I can't think of a way to pass the data (which rows to process) back to the new controller that will do the processing. 听起来很简单,但我想不出一种方法将数据(要处理的行)传递回进行处理的新控制器。 The data is in an array (array of id's) and there is too much to put as variables on the end of the url as the array may have up to 400 entries 数据位于一个数组(id的数组)中,并且作为变量放在url的末尾有太多内容,因为该数组最多可以包含400个条目

The only way I can think to do this seems horrible. 我认为做到这一点的唯一方法似乎太可怕了。 I could put the array into a single cell of a database table using json encode and give this table row an id. 我可以使用json编码将数组放入数据库表的单个单元格中,并为此表行指定一个ID。 I can then pass the id from the view to the new controller and retrieve the array. 然后,我可以将ID从视图传递到新控制器并检索数组。 There must be a better way? 一定会有更好的办法?

Move the code that parses the CSV and builds your array into its own function. 移动解析CSV的代码,并将数组构建到其自己的函数中。 We'll say you are creating a _parse_csv() method that will accept $filename as a parameter and then read the csv, process data, and return an array. 我们将说您正在创建一个_parse_csv()方法,该方法将接受$filename作为参数,然后读取csv,处理数据并返回一个数组。 You can use var_export and write the processed array to disk and load in your _parse_csv method instead of reprocessing. 您可以使用var_export并将已处理的阵列写入磁盘并以_parse_csv方法加载,而无需重新处理。

private function _parse_csv($filename) {
    // do some stuff with the uploaded csv here
    // return an array of data from the csv
    return $csv_array;
}

Call this method on csv upload and pass the uploaded filename to it. 在csv上传中调用此方法,并将上传的文件名传递给它。

Your process data button can pass the $filename to the controller/method that will parse the CSV into an array for processing. 您的process data按钮可以将$filename传递给控制器​​/方法,该控制器/方法会将CSV解析为一个数组以进行处理。

Use a class property to define the upload folder. 使用类属性定义上传文件夹。 Do not pass the upload folder when processing, only pass the filename. 处理时不传递上传文件夹,仅传递文件名。

WARNING: This option as discussed is NOT secure. 警告: 如讨论这个选项是不安全的。 You will need to take extra precaution to ensure that a user can't kick off the csv processing by guessing the filename parameter. 您将需要采取额外的预防措施,以确保用户无法通过猜测filename参数来启动csv处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM