Skip to content
Advertisement

php mysql export excel

This code is working without problem if page doesn’t contain any other content. When i add a html form, it gives only html content to excel. I need to use that form to customize query. How can i make this code working with html content?

<?php
if (isset($_POST['excel'])){
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
mysql_query("SET NAMES 'LATIN5'");
mysql_query("SET character_set_connection = 'LATIN5'");
mysql_query("SET character_set_client = 'LATIN5'");
mysql_query("SET character_set_results = 'LATIN5'");

$select = "query here";

$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );


$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 ) ) || ( $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/octet-stream");
header("Content-Disposition: attachment; filename=your_desired_name.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$headern$data"; }

?>

Advertisement

Answer

As mentioned in the comments, combining HTML and CSV is not possible

However, it seems you’re only outputing CSV if a form is posted ($_POST[‘excel’]). You should ‘stop’ the PHP script after outputting the CSV, like this:

if($_POST['excel']) {
    // query and output CSV 
    //...


    exit(); //done outputting csv
}

// regular HTML form here; will only be output if NO form is sent

With this approach, your script will:

  • output a HTML form if visited without sending a form
  • output CSV if the form has been submitted.

However, you may re-consider putting both functionality in the same PHP script. It’s probably a better approach to output the CSV in another script (e.g. download_csv.php) and set the ‘action’ of your form to that location;

<form action='download_csv.php'>

[update] To output HTML or CSV depending on the form-input, you may consider this:

if($_POST['excel']) {
    // query and output CSV 
    include 'csv-exporter.php';
    exit(); //done outputting csv
} else if ($_POST['html']) {
    // query and output HTML
    include 'html-exporter.php';
    exit(); //done outputting html
}

However, this way the ‘query’ part will be duplicated. In which case, it’s probably best to separate the querying and ‘formatting’ parts. You can do so by putting them in a function and use the result of that, something like:

if($_POST['excel']) {
    outputCSV(getResultsFromDatabase());
    exit();
} else if ($_POST['html']{
    outputHtmlList(getResultsFromDatabase());
    exit();
}

Those functions can be put in separate files and included using the ‘include’ functionality of PHP.

Both are just for illustration all purposes, and a bit off-topic for your original question.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement