简体   繁体   中英

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.

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

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. I can then pass the id from the view to the new controller and retrieve the array. There must be a better way?

Move the code that parses the CSV and builds your array into its own function. 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. You can use var_export and write the processed array to disk and load in your _parse_csv method instead of reprocessing.

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.

Your process data button can pass the $filename to the controller/method that will parse the CSV into an array for processing.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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