Page 1 of 1

function query

Posted: Fri Dec 11, 2009 10:36 am
by IGGt
I have a page that has a list of links on it (reports.php), each one connects to a .php page that puts together a MySQL query (e.g. j1report.php, j2report.php) (example below). That page then passes the query to another page (exportcsv.php) which exports the result to a csv file.

The problem is on this last page it creates a function (exportMysqlToCsv) that sets the filename to 'csvfile.csv'

I need to be able to pass the filename across like I would the query, but it doesn't work.

Code: Select all

 
<?php
//Create the MySQL Query
error_reporting(E_ALL ^ E_NOTICE);
//Set variables
    $uName = "root";
    $pWord = "";
    $host = "localhost:3306";
    $db = $_POST['DBSchema'];
    $Query = "...Query...;";
 
    $connection = mysql_connect($host, $uName, $pWord) ;
    mysql_select_db($db, $connection) ;
    require 'exportcsv.php';
    exportMysqlToCsv($Query);

Code: Select all

 
<?php
//code to create the csv file
function exportMysqlToCsv($Query,$filename = 'csvfile.csv')
{
    $csv_terminated = "\n";
    $csv_separator = ",";
    $csv_enclosed = '"';
    $csv_escaped = "\\";
    $sql_query = $Query;
 
    // Gets the data from the database
    $result = mysql_query($sql_query);
    $fields_cnt = mysql_num_fields($result);
    $schema_insert = '';
    for ($i = 0; $i < $fields_cnt; $i++)
    {
        $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
            stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
        $schema_insert .= $l;
        $schema_insert .= $csv_separator;
    } // end for
    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;
    // Format the data
    while ($row = mysql_fetch_array($result))
    {
        $schema_insert = '';
        for ($j = 0; $j < $fields_cnt; $j++)
        {
            if ($row[$j] == '0' || $row[$j] != '')
            {
                if ($csv_enclosed == '')
                {
                    $schema_insert .= $row[$j];
                } else
                {
                    $schema_insert .= $csv_enclosed .
                    str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                }
            } else
            {
                $schema_insert .= '';
            }
            if ($j < $fields_cnt - 1)
            {
                $schema_insert .= $csv_separator;
            }
        } // end for
        $out .= $schema_insert;
        $out .= $csv_terminated;
    } // end while
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: " . strlen($out));
    // Output to browser with appropriate mime type, you choose ;)
    //header("Content-type: text/x-csv");
    //header("Content-type: text/csv");
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=$filename");
    echo $out;
    exit;
}
?>
 
The problem as I see it as at the top of the second page (function exportMysqlToCsv($Query,$filename = 'csvfile.csv')

I have tried creating a variable before it ($time = getdate(); then filename = $time.'_.csv'). But this doesn't work. I just get an error message (Parse error: parse error, expecting `')'' on line 2).

I tried setting the variable in the first file and passing it across (exportMysqlToCsv($Query,$filename); But that just creates a filename of Array_.csv.

How can I change the filename to something useful?

Re: function query

Posted: Fri Dec 11, 2009 11:44 am
by AbraCadaver
Uhhh, why not pass the filename as the function allows you to do?

Code: Select all

exportMysqlToCsv($Query, 'whatever_you_want.csv');

Re: function query

Posted: Mon Dec 14, 2009 5:36 am
by IGGt
That would be fine if I knew what the filename was going to be, unfortunately I don't as it is generated at the time the script is run.

e,g. if I run j1report.php then the filename needs to be j1report_dateTime.csv, if I run j2report.php it needs to be j2report_dateTime.csv and so on.

Re: function query

Posted: Mon Dec 14, 2009 6:34 am
by AbraCadaver
IGGt wrote:That would be fine if I knew what the filename was going to be, unfortunately I don't as it is generated at the time the script is run.

e,g. if I run j1report.php then the filename needs to be j1report_dateTime.csv, if I run j2report.php it needs to be j2report_dateTime.csv and so on.

Code: Select all

$uName = "root";
$pWord = "";
$host = "localhost:3306";
$db = $_POST['DBSchema'];
$Query = "...Query...;";
$Filename = str_replace('.php', '', basename(__FILE__)) . '_' . time() . '.csv';
 
$connection = mysql_connect($host, $uName, $pWord) ;
mysql_select_db($db, $connection) ;
require 'exportcsv.php'; 
exportMysqlToCsv($Query, $Filename);

Re: function query

Posted: Mon Dec 14, 2009 7:48 am
by IGGt
I can see where I was going wrong now, my date() function wasn't quite how it should be