Page 2 of 3
Re: Create a csv file
Posted: Sun Apr 17, 2011 3:19 pm
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
Re: Create a csv file
Posted: Tue Apr 19, 2011 8:25 am
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!
Re: Create a csv file
Posted: Tue Apr 19, 2011 9:25 am
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
Re: Create a csv file
Posted: Tue Apr 19, 2011 9:43 am
by Flashart
Hi Lee
Printing to the browser works fine. I get the results in a html table. Not sure if this helps?!
Re: Create a csv file
Posted: Tue Apr 19, 2011 10:08 am
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
Re: Create a csv file
Posted: Tue Apr 19, 2011 10:24 am
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!
Re: Create a csv file
Posted: Tue Apr 19, 2011 10:32 am
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
Re: Create a csv file
Posted: Tue Apr 19, 2011 10:40 am
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.
Re: Create a csv file
Posted: Tue Apr 19, 2011 11:00 am
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
Re: Create a csv file
Posted: Tue Apr 19, 2011 11:05 am
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?!
Re: Create a csv file
Posted: Tue Apr 19, 2011 11:17 am
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, ',', '"' );
Re: Create a csv file
Posted: Tue Apr 19, 2011 11:21 am
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!
Re: Create a csv file
Posted: Tue Apr 19, 2011 11:30 am
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
Re: Create a csv file
Posted: Tue Apr 19, 2011 11:37 am
by Flashart
Hi Lee
The foreach definitely works as it prints it out to the screen.
This is partly why I am confused.
Re: Create a csv file
Posted: Tue Apr 19, 2011 11:43 am
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