MushMX Posted August 31, 2023 Share Posted August 31, 2023 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! Quote Link to comment Share on other sites More sharing options...
Barand Posted August 31, 2023 Share Posted August 31, 2023 The code I gave you in your previous topic downloads the file. 1 Quote Link to comment Share on other sites More sharing options...
MushMX Posted September 1, 2023 Author Share Posted September 1, 2023 i tried, but doesn’t fit with this code (is part of bigger code on wordpress) and corrupts all page. probably im doing it wrong, how do you integrate the download part on this code? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 1, 2023 Share Posted September 1, 2023 Have you tried adding a "download" attribute to the <a> element? eg <a href="filename" download >Download fiename</a> Quote Link to comment 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.