Offer HTML/PHP table as downloadable 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

Post Reply
User avatar
sunegtheoverlord
Forum Commoner
Posts: 28
Joined: Thu Feb 18, 2010 5:02 am

Offer HTML/PHP table as downloadable CSV file

Post by sunegtheoverlord »

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
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Offer HTML/PHP table as downloadable CSV file

Post by yacahuma »

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

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;

        }

User avatar
sunegtheoverlord
Forum Commoner
Posts: 28
Joined: Thu Feb 18, 2010 5:02 am

Re: Offer HTML/PHP table as downloadable CSV file

Post by sunegtheoverlord »

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.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Offer HTML/PHP table as downloadable CSV file

Post by yacahuma »

sorry

this line is wrong

Code: Select all

echo $output =  "data1,data2,data3";
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
User avatar
sunegtheoverlord
Forum Commoner
Posts: 28
Joined: Thu Feb 18, 2010 5:02 am

Re: Offer HTML/PHP table as downloadable CSV file

Post by sunegtheoverlord »

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?

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;

        }
User avatar
sunegtheoverlord
Forum Commoner
Posts: 28
Joined: Thu Feb 18, 2010 5:02 am

Re: Offer HTML/PHP table as downloadable CSV file

Post by sunegtheoverlord »

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?
Post Reply