Re: Another question about functions...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





What's happening is, I have the code set and it downloads the file into excel, but it doesn't have the database fields in it, rather a copy of the entire webpage which it trys to put into excel. Below is the code that I am using in my function to export the records:

<?PHP
    function excelexportfunc($select, $sortOrder, $exportdate) {

echo $select . "<br/>\n";

if you run that through your db manually do you get an error?

        $export = mysql_query($select);

var_dump($export);

        $fields = mysql_num_fields($export);

// initialize the header line to be an empty string.
$header = '';

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

You will need a trim here otherwise you'll have an extra empty column (extra \t in there):

$header = trim($header);

        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/vnd.ms-excel"); header("Content-Disposition: attachment; filename=Export.".$exportdate.".xls");
        header("Pragma: no-cache");
        header("Expires: 0");
print "$header\n$data";

Once you've printed out the report, you should probably exit so nothing else is processed from the script.

    }

?>

I am calling the function like so: "excelexportfunc($select, $sortOrder, $exportdate);"

the $select is specified in an IF statement on the calling page like so:

    if($exportoption =="all"){
        $sortOrder= $_SESSION['order'];
        $search = "";
        $select = "SELECT * FROM ".$table." order by ".$sortOrder."";

    }else{

        $sortOrder = $_SESSION['order'];
        $search = $_SESSION['search'];
$select = "SELECT * FROM ".$table." WHERE FName like '%".$search."%' or LName like '%".$search."%' or Add1 like '%".$search."%' or Add2 like '%".$search."%' or City like '%".$search."%' or State like '%".$search."%' or Zip like '%".$search."%' or XCode like '%".$search."%' order by ".$sortOrder."";
    }

$sortOrder is now part of $select you don't need to pass it to the function unless you actually use it in that function (which from your copy/paste isn't the case).


So your function can just be:

function excelexportfunc($select, $exportdate) {

and remove the $sortOrder from the calling lines:

excelexportfunc($select, $exportdate);

--
Postgresql & php tutorials
http://www.designmagick.com/

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux