I'm trying to import a load of data into a sub fields of a repeater field (mortgage_providers) using file field (dd_csv) to upload the file to a post and return an array. Then loop through each row with PHP and update the repeater fields. I have tried the following but I can't seem to get it to work.
if( get_field('dd_csv') ) {
// load csv with SERVER PATH instead of URL
$csv = get_attached_file(get_field('dd_csv')['id']);
if(($handle = fopen($csv, "r")) !== FALSE) {
$count = 0;
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// $count = acf row, $data[0] = csv column 1 value
update_sub_field(array('mortgage_providers', $count, 'mortgage_provider_name'), $data[0], 'option');
$count++;
}
fclose($handle);
}
}
Here's a sample of the data:
In total there's over 1000 rows in column 1 of the CSV.
Thanks
I would hook into the on save action :
<?php
add_action( 'save_post', 'prefix_check_files', 10, 3 );
function prefix_check_files( $post_id, $post, $update ) {
if( get_field('dd_csv', $post_id) ) {
// load csv with SERVER PATH instead of URL
$csv = get_attached_file(get_field('dd_csv', $post_id)['id']);
if(($handle = fopen($csv, "r")) !== FALSE) {
$count = 0;
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// $count = acf row, $data[0] = csv column 1 value
update_sub_field(array('mortgage_providers', $count, 'mortgage_provider_name'), $data[0], 'option');
$count++;
}
fclose($handle);
}
}
}
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.