Jump to content

file_put_contents adding duplicate data


jarvis

Recommended Posts

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

Link to comment
Share on other sites

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"

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?
  • Thanks 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.