Jump to content

Exporting Multiple CSVs from Multiple Tables


smidgen11
 Share

Recommended Posts

I am trying to export multiple csv files to download from 2 separate tables in my database.

 

Some background:

I have a web app that links to a phpmyadmin database. There are 2 tables in the database (entering and exiting). These tables hold the phone inventory information of employees currently entering or exiting the organization.

 

The fields in my tables are:

 

location, firstname, lastname, username, extension, mac, type

 

What I am trying to do is export the data in these 2 tables to CSV (which I have working now) but I need to have multiple CSVs for each.

 

For example, for my exiting table, I need to have one CSV export all the fields in the table and I also need another CSV to export just the location and username field. I have a simple html form with a submit button which is currently working now:

 

<form action="exportexiting.php" method="POST" style="width: 456px; height: 157px">

<div>

<fieldset>

<legend style="width: 102px; height: 25px"><strong>Entering CSV:</strong></legend>

<input name="Export" type="submit" id="Export" value="Export">

</fieldset><br/>

</div>

</form>

 

 

This links to my exportexiting.php file:

 

<?php

$host = 'localhost';

$user = 'root';

$pass = 'xxxx';

$db = 'PhoneInventory';

$table = 'exiting';

 

 

 

// Connect to the database

$link = mysql_connect($host, $user, $pass);

mysql_select_db($db);

 

require 'exportcsv.inc.php';

 

 

exportMysqlToCsv($table);

 

 

 

?>

 

Then to exportcsv.inc.php:

 

<?php

 

function exportMysqlToCsv($table,$filename = 'export.csv')

{

    $csv_terminated = "\n";

    $csv_separator = ",";

    $csv_enclosed = '';

    $csv_escaped = "\\";

    $sql_query = "select * from $table";

 

    // 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;

 

}

 

?>

 

 

 

Again, this is working perfectly for only exporting all the data from the exiting table into one CSV file. My question is, how can I make my one submit button export the 2 CSV files that I need?

 

 

Thanks for the help...

 

 

Link to comment
Share on other sites

The hard option would be to create the two export files and put them in a zip archive and then download that to the user. A better option, in my opinion, is to simply have two links on the page (i.e. no form/button). Each link will have a target of "_blank" and the href will point to the exportexiting.php page, each with an additional parameter on the URL to specify which report to generate.

 

Then the user simply has to click each link to download the report(s) they need.

Link to comment
Share on other sites

Unfortunately, due to the potential of needed more (than 2) reports from one table in the future, I think the zip option is going to be the way to go. Ive been google'ing the zip option for days but cant seem to get anywhere. Do you know of an example of this somewhere on the web?

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.