views:

20

answers:

2

I don't know the best way to title this question but am trying to accomplish the following goal:

When a client logs into their profile, they are presented with a link to download data from an existing database in CSV format. The process works, however, I would like for this data to be 'fresh' each time they click the link so my plan was - once a user has clicked the link and downloaded the CSV file, the database table would 'erase' all of its data and start fresh (be empty) until the next set of data populated it.

My EXISTING CSV creation code:

<?php
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'database';
$table = 'tablename';
$file = 'export';



$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;
}
}
$csv_output .= "\n";

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= '"'.$rowr[$j].'",'; 
}
$csv_output .= "\n";
}

$filename = $file."_".date("Y-m-d",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>

any ideas?

A: 

i'm extremely confused as to why the data needs to be 'fresh' and why you have a database table that can be cleared out at will.

can't you just export whatever data you want directly to a CSV and skip the whole writing-to-and-subsequently-deleting-the-db step?

I would think that whatever data you're collecting and storing in this 'temporary table' can be accumulated elsewhere, and then when they want the CSV, you can just get all the data you need at that time instead of keeping around a pointless table.

contagious
@contagious - why is this 'extremely' confusing. Because the client has ZERO technical ability and will not want to pull registration information from a database that would house 150+ members per day. That then means they have to check off those that they have manually entered into their own systems and HOPE they got it right the first time. The file is ALREADY exporting to CSV as I mention above. The table is not pointless. Your answer is
JM4
+1  A: 
$query = mysql_query('TRUNCATE TABLE '.$table);

or if you want to archive them

$query = mysql_query('RENAME TABLE db_name.'.$table.' TO db_name.'.$table.time());
$query = mysql_query('CREATE TABLE db_name.'.$table.' ( /* table structure */ )');
UltimateBrent
@UltimateBrent - Thank you for this! As I understand from your code, to archive I will be renaming the table entirely? It would be great if there were 2 databases - one with new data, one with ALL data. New data is flushed after each viewing; ALL DATA is populated with any flushed information from the 'NEW DATA" table. Possible?
JM4
NEVERMIND - GOT IT!
JM4