Hello,
I've got a php page that contains an html table that in turn populates from a SQL stored procedure that queries a number of tables and a number of databases.
The table populates fine and all i want to do is offer the user the chance to download this table as a csv file.
I've been reading around related posts but can't find or understand how to do it.
Can someone point me in the right direction in idiot speak please?
Thanks
S
Offer HTML/PHP table as downloadable CSV file
Moderator: General Moderators
- sunegtheoverlord
- Forum Commoner
- Posts: 28
- Joined: Thu Feb 18, 2010 5:02 am
Re: Offer HTML/PHP table as downloadable CSV file
You have to create a new version of your code that write a CSV instead of html. It has to use the same parameters you use to select the data in the html version
Page 1: Html version
<a href="csvversion.php?params=xxxx"
Html version of table here
Page 2: csvversion.php
Page 1: Html version
<a href="csvversion.php?params=xxxx"
Html version of table here
Page 2: csvversion.php
Code: Select all
session_cache_limiter('private');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.177
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
echo $output = "data1,data2,data3";
$name =randomString();
if ($format == 'txt')
{
header("Content-type: application/text; charset=UTF-8");
header("Content-Disposition: attachment; filename={$name}.txt");
echo $output;
exit;
}
else if ($format == 'xls')
{
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename={$name}.csv");
echo $output;
exit;
}
- sunegtheoverlord
- Forum Commoner
- Posts: 28
- Joined: Thu Feb 18, 2010 5:02 am
Re: Offer HTML/PHP table as downloadable CSV file
Hi Yacahuma,
Thanks for the reply. I am a total beginner in PHP.
I've done what you suggested but i keep getting the error:-
Fatal error: Call to undefined function session_cache_limiter() in *HTDOCS* on line 155
Is this something to do with the version of PHP? i'm using 5.2.8.
Thanks for the reply. I am a total beginner in PHP.
I've done what you suggested but i keep getting the error:-
Fatal error: Call to undefined function session_cache_limiter() in *HTDOCS* on line 155
Is this something to do with the version of PHP? i'm using 5.2.8.
Re: Offer HTML/PHP table as downloadable CSV file
sorry
this line is wrong
I just wanted to say that in that section you will create your own data
session_cache is an old function. You should have no problems.
Can you post your new code
this line is wrong
Code: Select all
echo $output = "data1,data2,data3";
session_cache is an old function. You should have no problems.
Can you post your new code
- sunegtheoverlord
- Forum Commoner
- Posts: 28
- Joined: Thu Feb 18, 2010 5:02 am
Re: Offer HTML/PHP table as downloadable CSV file
Hi,
I understand that i have to put my own data in that line and have done. but i'm still getting
Fatal error: Call to undefined function session_cache_limiter() in /usr/home/toby/enquiries/htdocs/mimain_report_results.php on line 154
I still don't understand why i'm getting this error, could you explain?
are you saying that i shouldn't be getting this error because its an old function? or are you saying that i must replace this with a new function?
I understand that i have to put my own data in that line and have done. but i'm still getting
Fatal error: Call to undefined function session_cache_limiter() in /usr/home/toby/enquiries/htdocs/mimain_report_results.php on line 154
I still don't understand why i'm getting this error, could you explain?
are you saying that i shouldn't be getting this error because its an old function? or are you saying that i must replace this with a new function?
Code: Select all
session_cache_limiter('private');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.177
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
echo $output = "{$PropsIn},{$PropsInAdam},{$PropsInEmma}";
$name =randomString();
if ($format == 'txt')
{
header("Content-type: application/text; charset=UTF-8");
header("Content-Disposition: attachment; filename={$name}.txt");
echo $output;
exit;
}
else if ($format == 'xls')
{
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename={$name}.csv");
echo $output;
exit;
}- sunegtheoverlord
- Forum Commoner
- Posts: 28
- Joined: Thu Feb 18, 2010 5:02 am
Re: Offer HTML/PHP table as downloadable CSV file
Hello again,
Ignore my last post, i've managed to update an extension in the php.ini file that has got rid of that error.
However, now nothing appears to happen on the csvversion.php page? How do i get this to offer a download?
Ignore my last post, i've managed to update an extension in the php.ini file that has got rid of that error.
However, now nothing appears to happen on the csvversion.php page? How do i get this to offer a download?