Re: Another question about functions...

From: Date: Wed, 30 Jan 2008 06:22:09 +0000
Subject: Re: Another question about functions...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-268303@lists.php.net to get a copy of this message
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/

« previous php.general (#268303) next »