jarvis Posted August 26, 2021 Share Posted August 26, 2021 Morning! I'm taking an array, looping through and placing the data into a CSV file. If I echo the foreach data, it only gets displayed once. If I open the CSV file, the same line is added numerous times. foreach( $orders[0] as $order ){ $result = '"'.$order->sku. '",'; $result .= '"'.$order->title.'"'; $result .= '"'.$order->first_name.'"'; $result .= '"'.$order->email.'"'; $result .= '"'.$order->country.'"'; $result .= '"'.$order->purchase_date.'"'; $result .= $order->order_ref; $result .= $order->customer_ref; $result .= number_format($order->price, 2, '.', ''); $result .= '"'.$order->currency.'"'; $result .= '"'.$order->delivery_date.'"'; $result .= '"'.$order->sales_channel.'"'; $result .= $order->member_id; $result .= $order->member_branch_id. "\n"; echo $result; file_put_contents( $order_data, $result, FILE_APPEND ); } Have I missed something? Surely if it's displayed once, it should only be added once to the CSV file? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/313605-file_put_contents-adding-duplicate-data/ Share on other sites More sharing options...
requinix Posted August 26, 2021 Share Posted August 26, 2021 Do you see the same line multiple times sequentially? Or are the bunch of lines repeated as a group? Quote Link to comment https://forums.phpfreaks.com/topic/313605-file_put_contents-adding-duplicate-data/#findComment-1589378 Share on other sites More sharing options...
jarvis Posted August 26, 2021 Author Share Posted August 26, 2021 It's exactly the same line but just repeated many, many times. For example: "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" Quote Link to comment https://forums.phpfreaks.com/topic/313605-file_put_contents-adding-duplicate-data/#findComment-1589380 Share on other sites More sharing options...
requinix Posted August 26, 2021 Share Posted August 26, 2021 You're only giving the barest description of this "added numerous times" deal. Does that sort of pattern happen for every other (different) line in the file too? In the same way? Are they repeated the same number of times? Is each distinct line repeated always consecutively or are they ever mixed? Any discernable pattern? Any breaks in that pattern? Quote Link to comment https://forums.phpfreaks.com/topic/313605-file_put_contents-adding-duplicate-data/#findComment-1589381 Share on other sites More sharing options...
jarvis Posted August 26, 2021 Author Share Posted August 26, 2021 Sorry! No, it's exactly the same. Whilst the output of the loop on the screen shows 1 result, the file seems to be on a continuous loop and will keep adding exactly the same information. It's never mixed, it doesn't alter or deviate. There are no breaks Which is why I'm stumped. If it outputs once on the screen and the file_put_contents is in the same loop, surely it should only write the same number of times it appears on the screen!? Quote Link to comment https://forums.phpfreaks.com/topic/313605-file_put_contents-adding-duplicate-data/#findComment-1589382 Share on other sites More sharing options...
Barand Posted August 26, 2021 Share Posted August 26, 2021 Your $result string contains only one comma (after the SKU). Where do the others come from in your listed output? Are you sure that's the code causing the problem? Quote Link to comment https://forums.phpfreaks.com/topic/313605-file_put_contents-adding-duplicate-data/#findComment-1589384 Share on other sites More sharing options...
jarvis Posted August 26, 2021 Author Share Posted August 26, 2021 Hi @Barand I realised this when I went to post the example of data, so rectified it. My full code looks like this: <?php add_action( 'wp_ajax_nopriv_get_batch_orders_from_api', 'get_batch_orders_from_api' ); add_action( 'wp_ajax_get_batch_orders_from_api', 'get_batch_orders_from_api' ); function get_batch_orders_from_api() { // get yesterdays file $yesterdays_filename = "purchase_history_".date('Ymd',strtotime("-1 days"))."_001.csv"; $previous_data_file = get_stylesheet_directory() . '/files/'.$yesterdays_filename; // delete yesterdays file if(file_exists($previous_data_file)){ unlink( $previous_data_file ); echo 'The file ' . $previous_data_file . ' has been removed!<br/>'; } // create todays file $filename = "purchase_history_".date("Ymd")."_001.csv"; $order_data = get_stylesheet_directory() . '/files/'.$filename; if (file_exists($order_data)) { echo 'The file ' . $filename . ' already exists, data will now append the file<br/>'; } else { echo 'The file ' . $filename . ' does not exist and has been created!<br/>'; $headers = '"SKU",'; $headers .= '"Title",'; $headers .= '"First_Name",'; $headers .= '"Email",'; $headers .= '"Country",'; $headers .= '"PurchaseDate",'; $headers .= '"OrderRef",'; $headers .= '"CustomerRef",'; $headers .= '"Price",'; $headers .= '"Currency",'; $headers .= '"DeliveryDate",'; $headers .= '"SalesChannel"'; file_put_contents( $order_data, $headers . "\n", FILE_APPEND ); } $orders = []; // Should return an array of objects $results = wp_remote_retrieve_body( wp_remote_get('https://www.domain.com/batch-orders.php') ); // turn it into a PHP array from JSON string $results = json_decode( $results ); // Either the API is down or something else spooky happened. Just be done. if( ! is_array( $results ) || empty( $results ) ){ return false; } $orders[] = $results; foreach( $orders[0] as $order ){ $result = '"'.$order->sku. '",'; $result .= '"'.$order->title.'",'; $result .= '"'.$order->first_name.'",'; $result .= '"'.$order->email.'",'; $result .= '"'.$order->country.'",'; $result .= '"'.$order->purchase_date.'",'; $result .= $order->order_ref.','; $result .= $order->customer_ref.','; $result .= number_format($order->price, 2, '.', ''); $result .= '"'.$order->currency.'",'; $result .= '"'.$order->delivery_date.'",'; $result .= '"'.$order->sales_channel. "\n"; echo $result; file_put_contents( $order_data, $result, FILE_APPEND | LOCK_EX ); } $current_page = $current_page + 1; wp_remote_post( admin_url('admin-ajax.php?action=get_batch_orders_from_api'), [ 'blocking' => false, 'sslverify' => false, // we are sending this to ourselves, so trust it. 'body' => [ 'current_page' => $current_page ] ]); } If I check the output of batch-orders.php. this is the data: [{"sku":"JK115-W","title":"","first_name":"John Smith","email":"name@domain.com","country":"United Kingdom","purchase_date":"14\/02\/2020","order_ref":"683","customer_ref":"683","price":"34.99","currency":"GBP","delivery_date":"15\/02\/2021","sales_channel":"Online"}] The output of $result is the following: "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" But as mentioned, the CSV file generated just seems to loop the same line over and over, yet only appears once. Hopefully, that helps? Quote Link to comment https://forums.phpfreaks.com/topic/313605-file_put_contents-adding-duplicate-data/#findComment-1589385 Share on other sites More sharing options...
Barand Posted August 26, 2021 Share Posted August 26, 2021 54 minutes ago, jarvis said: The output of $result is the following: "JK115-W","","John Smith","name@domain.com","United Kingdom","14/02/2020",683,683,34.99"GBP","15/02/2021","Online" No, it isn't. You'll find that the final " is missing after the "sales-channel" $result .= '"'.$order->sales_channel. "\n"; should be $result .= '"'.$order->sales_channel. "\"\n"; That aside, I can't see multiple writes in the function. My guesses at the moment It is being called multiple times, each call writing a single record. It is being call recursively (What does wp_remote_post( admin_url('admin-ajax.php?action=get_batch_orders_from_api') do inside the function? 1 Quote Link to comment https://forums.phpfreaks.com/topic/313605-file_put_contents-adding-duplicate-data/#findComment-1589387 Share on other sites More sharing options...
jarvis Posted August 26, 2021 Author Share Posted August 26, 2021 @Barand Yep, was being called recursively! #facePalm Thanks Quote Link to comment https://forums.phpfreaks.com/topic/313605-file_put_contents-adding-duplicate-data/#findComment-1589389 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.