|
Posted by nephish on 09/19/06 04:24
Petr Vileta wrote:
> nephish wrote:
> > Hello there,
> >
> > i have an app that i need to be able to publish a link to download a
> > csv file.
> > i know that i can use php to make the file, but how to i link to it
> > through php.
> > like if i have an html file that has a link in it like this
> > <a href="somedata.csv">here be data</a>
> > or would i make that a .php file that would generate a csv ?
> >
> > the data will come from MySQL.
> >
> > any ideas?
> >
> By me the best choice is to have page with links as php page and have other
> php page for generating and sending csv file direct to browser.
>
> download.php
> -------------------
> <html><body> ....
> <?php
> # link for csv where mysql query contain eg. "... WHERE id=12345"
> echo "<a href=\"sendcsv.php?id=12345\>here be data</a>\n";
> ?>
> </body></html>
>
> sendcsv.php
> -----------------
> <?php
> $link = mysql_connect("localhost", "user, "password") or die(mysql_error());
> mysql_select_db("mydb") or die(mysql_error());
> $id = $_GET["id"]*1; # multiply by 1 preempt sql injection
> $query = "SELECT * FROM mytable WHERE id=$id";
> $result = mysql_query($query);
> echo "Content-Type: application/csv\n",
> "Content-Disposition: filename=\"csv_data.csv\"\n\n";
> while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
> {
> echo $row["field1"], ';', $row["field2"], ';' ...."\n";
> }
> mysql_free_result($result);
> mysql_close($link);
> ?>
>
>
> --
>
> Petr Vileta, Czech republic
> (My server rejects all messages from Yahoo and Hotmail. Send me your mail
> from another non-spammer site please.)
cool, thanks a lot !
-sk
Navigation:
[Reply to this message]
|