On Fri, 8 Feb 2008, Jason Pruim wrote:


On Feb 8, 2008, at 10:14 AM, Hiep Nguyen wrote:


On Fri, 8 Feb 2008, Per Jessen wrote:

Hiep Nguyen wrote:

is there anyway to generate this into xls file w/o using fopen &
fwrite to the server?  my goal is to have a link after the table and
user can click on that link and a save window pop up to allow user to
save to local disk.

Yes - have a link like this:

<a href="<yourscript.php?parameters>">Get XLS file</a>

in yourscript.php, you evaluate the parameters given and build the XSL
file as output:

header("Content-Type: application/excel");
header("Content-Disposition: attachment; filename=\"filename\"");

print
.
.
.
.
.


done.

i already got this method, but the problem that i have is the parameters is mysql statement and it's very long. i don't think a good idea to pass this in the url. also, the page that i'm working on is a search page, therefore the mysql statement is very complicate and long. that's why i don't want to pass via url.

is there way to do within this page???


I have actually done what you are looking for... Here is how I do it:

On the search page, write the search phrase into a session variable so you can access it from another page and then run this code:

<?PHP

$sortOrder = $_SESSION['order'];
$search = $_SESSION['search'];
$select = "SELECT * FROM ".$table." WHERE blah blah blah blah blah"";

$export = mysql_query($select);
$fields = mysql_num_fields($export);

for ($i = 0; $i < $fields; $i++) {
        $header .= mysql_field_name($export, $i) . "\t";
}

while($row = mysql_fetch_row($export)) {
        $line = '';
        foreach($row as $value) {
                if ((!isset($value)) or ($value == "")) {
                        $value = "\t";
                }
                else
                {
                        $value = str_replace('"', '""', $value);
                        $value = '"' . $value . '"' . "\t";
                }                       $line .= $value;
        }
        $data .= trim($line). "\n";
}
$data = str_replace("\r", "", $data);

if ($data =="") {
        $data ="\n(0) Records Found!\n";
}
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=Export.xls");
header("Pragma: no-cache");
header("Expires: 0");


print "$header\n$data";


?>

What that does is it writes the current search pattern into an excel file called Export.xls and downloads it to your harddrive. I am in the process of re-writing it to work as a function, but have had mixed results so far. I'm learning functions right now :)

If you know functions, and want to take a stab at re-writing it, I can provide you with the code I have so far :)


thank you for the suggestion, but here is another problem that i try to avoid saving mysql statement to query from the table again.

let say that user searched and found 10 records,
in the meantime, other users may change any of these 10 records,
so if we saved mysql statement and re-run mysql statement again, the result might be different. to prevent this problem, i only want to download records that returned on this page only.

thanks,
t. hiep

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to