Create a csv file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Create a csv file

Post by incubi »

Well, I looked over the sample and I don't see anything strange about it. We need to add a little to the foreach. I haven't tested this so you will need to play around with it but try this.

Code: Select all

	foreach ($ga as $key => $value) 
	{
		foreach ($value as $iKey => $iValue) 
		{
			
		}
	}
And also look at this
http://php.net/manual/en/control-structures.foreach.php


Lee
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Create a csv file

Post by Flashart »

Hi Lee

I don't get error messages unless I put a string where an array should be within the foreach loop. What happens is the csv file is generated, but it's blank. So I need to sort out a way of printing out each line of the array. If that makes sense?

Thanks again for your help!
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Create a csv file

Post by incubi »

Hi,

Don't worry about the csv fie yet make sure the loops are work and just print the data to the browser.
What is the error message?

Lee
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Create a csv file

Post by Flashart »

Hi Lee

Printing to the browser works fine. I get the results in a html table. Not sure if this helps?!
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Create a csv file

Post by incubi »

Hmm, I thought it was an issue with the foreach printing the arrays.
In any case I still need the error but, if you can get the data to the browser then you're done.

In the "browser" :) view you must be printing or echoing the data just put that data in an array like

$csvarray[] =

Then use that array to write the csv.

Lee
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Create a csv file

Post by Flashart »

Yeah that's my issue. When I try the foreach array within the csv generation script the result is the csv is blank. I need a script that will generate a csv using the html table data.

I'm not sure how to do that part!
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Create a csv file

Post by incubi »

I don't understand, why parse the html to build an array to put in a csv file when you have the raw data and you can put it in an array directly and write that to a csv file? The html has nothing to do with the csv file.

Lee
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Create a csv file

Post by Flashart »

The html table side of things was prewritten by the gapi team and I know its completely optional. It's there purely for me to try and illustrate what I need in a csv file. So yes, all I am trying to do is print the contents of the array to a csv.
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Create a csv file

Post by incubi »

If the following loops gets the data like you said then this should work.

Code: Select all

        foreach ($ga as $key => $value)
        {
                foreach ($value as $iKey => $iValue)
                {
                       $csvarray[] =   $iValue;
                }
        }
       fputcsv($fp, $csvarray, ',', '"' );
And again I need to know the "error(s)" you get from the logs.

Lee
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Create a csv file

Post by Flashart »

The error I get from this is

Warning: fputcsv() expects parameter 1 to be resource, null given in /Applications/MAMP/htdocs/analytics/gapi-1.3/analytics.php on line 72

Not sure if i have any logs?!
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Create a csv file

Post by incubi »

That says your handle is not good. You get this error when using this code?

$fh = fopen("sales.csv", 'w') or die ("Can't open sales.csv");
foreach ($ga as $key => $value)
{
foreach ($value as $iKey => $iValue)
{
$csvarray[] = $iValue;
}
}
fputcsv($fh, $csvarray, ',', '"' );
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Create a csv file

Post by Flashart »

A new one..

Warning: fputcsv() expects parameter 2 to be array, null given in /Applications/MAMP/htdocs/analytics/gapi-1.3/analytics.php on line 73

Means nothing to me!
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Create a csv file

Post by incubi »

If the array is empty then the foreach is not working, you said it was and that you could print the data out to the browser.
You need to work it back start by printing $csvarray if its empty then print $ga then make one loop and print that data and so on...
I'm sure you have everything you need to make it work but you will need to go step by step.


Lee
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Create a csv file

Post by Flashart »

Hi Lee

The foreach definitely works as it prints it out to the screen.
This is partly why I am confused.
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Create a csv file

Post by incubi »

Yep, its a bummer. The only way I can do more is if I can recreate the issue on my system and to do that I will need everything you have involving this code. Other then that I don't know where to point you because I only have fragments of info.

Lee
Post Reply