Jump to content

Recommended Posts

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

	}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/317219-cant-make-csv-enlists-all-results/
Share on other sites

Just now, ginerjm said:

I personally have no idea what you are doing.

Basically export this information:

image.thumb.png.75d663632ad307ecfe9050b434048b0f.png

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:

image.thumb.png.058ccf207b3e5ab0990f7a3b478914dd.png

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

 

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);
    }
    
}

 

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 

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

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

 

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.