Jump to content

CSV export open CSV in the browser instead of download file


MushMX

Recommended Posts

Hi,

I hope everyone is having a great day.
Today i have an issue related with a CSV download/export

This is the code

	error_reporting(0);

	global $wpdb;

	$obj_membership=new MJ_gmgt_membership;

	$membershipdata=$obj_membership->MJ_gmgt_get_all_membership();
	
	$obj_product=new MJ_gmgt_product;
    
	$product = $obj_product->MJ_gmgt_get_single_product($retrieved_data->product_id);


	if(!empty($membershipdata))

	{

		$filename="Membership Report.csv";

		$fp = fopen($filename, "w");

		// Get The Field Name

		$output="";

		$header = array();
        
		$header[] = esc_html__('Id','gym_mgt');

		$header[] = esc_html__('Invoice No.','gym_mgt');

		$header[] = esc_html__('Member Name','gym_mgt');

		$header[] = esc_html__('Product Name','gym_mgt');

		$header[] = esc_html__('Total Amount','gym_mgt');
        
		$header[] = esc_html__('Paid Amount','gym_mgt');

		$header[] = esc_html__('Due Amount','gym_mgt');

		$header[] = esc_html__('Payment Status','gym_mgt');

		fputcsv($fp, $header);

		$i=1;

		$membership_id = $membershipdata->membership_id;

		$user = get_users(array('role' => 'member'));
		
		
		$invoice_no=$retrieved_data->invoice_no;

		foreach ($storedata as $retrieved_data)

		{

			$membership = $obj_membership->MJ_gmgt_get_single_membership($user_data->membership_id);

			$membership_name = $membership->membership_label;			

			$row = array();

			$row[] = $i;

			$row[] = $retrieved_data->invoice_no;

			$row[] = $userdata->display_name;

			$row[] = $product_name;

			$row[] = $retrieved_data->total_amount;

			$row[] = $retrieved_data->paid_amount;
            
			$row[] = $due_amount;
            
			$row[] = $retrieved_data->payment_status;

			$i++;

		fputcsv($fp, $row);

		}

		// Download the file
		fclose($fp);

	   ?>

		<?php

	}

?>

<div class="margin_top_20 form-group col-md-4 col-xs-12 button-possition ">

	<a class="btn report_download_btn" href='<?php echo $filename;?>'><?php esc_html_e('Download Report In CSV','gym_mgt');?></a>

</div>


Basically is exporting the CSV, but is opening itself on the web browser instead of get a downloadable file

Thanks in advance for your time and help best regards!

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.