MushMX Posted August 24, 2023 Share Posted August 24, 2023 Hi everyone, I have a code issue, below you will find a portion of code. the code works "fine"partially, because only deliver a CSV with the latest value of the table only. in this case i have 3 registry values, but the code only deliver last one, the others are not delivered or included. could you please help me out to make csv deliver all results on my table please? or give me ideas to do this? thanks in advance best regards, and many thanks to read and take time to check this <?php 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=>Product Quantity','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')); $forechid = $retrieved_data->invoice_no; foreach ($user as $user_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[] = $user_data->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 } ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 24, 2023 Share Posted August 24, 2023 I personally have no idea what you are doing. Quote Link to comment Share on other sites More sharing options...
MushMX Posted August 25, 2023 Author Share Posted August 25, 2023 Just now, ginerjm said: I personally have no idea what you are doing. Basically export this information: Into a csv file. this get filed names: // 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=>Product Quantity','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'); and this one (where i personally think the problem is, but i dont know where) enlist all information and export it to a csv $i=1; $membership_id = $membershipdata->membership_id; $user = get_users(array('role' => 'member')); $forechid = $retrieved_data->invoice_no; foreach ($user as $user_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[] = $user_data->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 } ?> the final result on the csv is: as you can see, meets only the last table result, and its skipinng the first one. i need help making the csv enlist ALL results, not only the last one Quote Link to comment Share on other sites More sharing options...
Barand Posted August 25, 2023 Share Posted August 25, 2023 I too don't know what your esoteric functions are returning but here is the code I have been using for years to produce data for my christmas card address labels $db = new mysqli(HOST,USERNAME,PASSWORD,'mydb'); $y = date("Y"); $filename = "xmaslist_$y.csv"; $sql = "SELECT family_vw.addressee , add1 , add2 , town , county , postcode , CASE WHEN xmas.post=1 THEN 'Y' ELSE '' END as post , xmas.cardcategory FROM xmas INNER JOIN family_vw USING (family_id) ORDER BY xmas.cardcategory "; sql2csv($db, $sql, $filename,); /** * Write query output to csv file for export * * Parameters * $conn - database connection * $sql - the sql query to be executed * $filename - name of download file (default "download_yymmddhhii.csv") * $headings - 1 if fieldname headings required (default), 0 if not required */ function sql2csv($conn, $sql, $filename='', $headings=1) { if (!$filename) $f = 'download_' . date('ymdhi') . '.csv'; else $f = $filename; $fp = fopen('php://output', 'w'); // so you can fputcsv to STDOUT if ($fp) { $res = $conn->query($sql); if ($res) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="'.$f.'"'); header('Pragma: no-cache'); header('Expires: 0'); $row = $res->fetch_assoc(); if ($headings) { fputcsv($fp, array_keys($row)); } do { fputcsv($fp, $row); } while ($row = $res->fetch_assoc()); } else echo "Error in query"; fclose($fp); } } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 25, 2023 Share Posted August 25, 2023 You're talking about taking some (printed?) data and trying to read it from whatever media it is in and writing it out to a text file? Sounds simple except for the input format. That (to me) seems to be the difficult part. So - to simplify this: 1 - determine how to read the input source 2 - how to recognize the individual pieces of data 3 - compose a text string of those pieces 4 - write 5 - repeat Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 25, 2023 Share Posted August 25, 2023 (edited) Mis-read the code that I thought had the error Edited August 25, 2023 by ginerjm Decided to step back Quote Link to comment Share on other sites More sharing options...
MushMX Posted August 25, 2023 Author Share Posted August 25, 2023 10 hours ago, Barand said: I too don't know what your esoteric functions are returning but here is the code I have been using for years to produce data for my christmas card address labels $db = new mysqli(HOST,USERNAME,PASSWORD,'mydb'); $y = date("Y"); $filename = "xmaslist_$y.csv"; $sql = "SELECT family_vw.addressee , add1 , add2 , town , county , postcode , CASE WHEN xmas.post=1 THEN 'Y' ELSE '' END as post , xmas.cardcategory FROM xmas INNER JOIN family_vw USING (family_id) ORDER BY xmas.cardcategory "; sql2csv($db, $sql, $filename,); /** * Write query output to csv file for export * * Parameters * $conn - database connection * $sql - the sql query to be executed * $filename - name of download file (default "download_yymmddhhii.csv") * $headings - 1 if fieldname headings required (default), 0 if not required */ function sql2csv($conn, $sql, $filename='', $headings=1) { if (!$filename) $f = 'download_' . date('ymdhi') . '.csv'; else $f = $filename; $fp = fopen('php://output', 'w'); // so you can fputcsv to STDOUT if ($fp) { $res = $conn->query($sql); if ($res) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="'.$f.'"'); header('Pragma: no-cache'); header('Expires: 0'); $row = $res->fetch_assoc(); if ($headings) { fputcsv($fp, array_keys($row)); } do { fputcsv($fp, $row); } while ($row = $res->fetch_assoc()); } else echo "Error in query"; fclose($fp); } } Honestly me neither :S its not my own code im trying to adapt a function that is missing , and accomplish almost all, except enlist all rows :S but your code could be an option i will try it, but idk if i need the sql connection, since with my code the connection with tables is already open without use the login details on plain text, however if i cant move forward, i think i can use this. My only concern is WHY i cant enlist all just last result, and its particullar thats only last one, not the first for example Quote Link to comment Share on other sites More sharing options...
MushMX Posted August 25, 2023 Author Share Posted August 25, 2023 7 hours ago, ginerjm said: Mis-read the code that I thought had the error share your ideas, even if you think couldnt work, probably could help. as something that happen, on the for each (user as userdata) if i change, the csv stop throw results, and if i switch userdata for another value the USERNAME result change, for example if i set user as invoice_no the user name get switched to 00005 but its still only displaying last result only. i dont know how figure out this 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.