<?php
include("scripts/mysql2csv.php");
$databasename='temp'; //delete this line (its just for my server config)
include("../../Scripts/connect.php"); //connect to database
//////////////////////////////// CREATE A DEMO TABLE: ////////////////////////////////
//This bit just creates a table called newsdemo for the demo with 2 records:
if(!mysql_query("DESC newsdemo;")){
mysql_query("CREATE TABLE `newsdemo` (
`newsID` bigint(20) NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`content` longtext NOT NULL,
`date` date NOT NULL default '0000-00-00',
`pic` varchar(255) NOT NULL default '',
`event` binary(1) default '0',
`current` binary(1) NOT NULL default '1',
PRIMARY KEY (`newsID`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;") or die(mysql_error());
mysql_query("INSERT INTO `newsdemo` VALUES ('1','Test1','The first test','2009-10-04','testno1.jpg','1','0'), ('2','Test2','Content for test2','2009-10-04','pic2.jpg','1','1');") or die(mysql_error());
}
////////////////////////////////////////////////////////////////////////////////////
switch($_REQUEST['s']){
case 'exportall'://EXPORT ENTIRE TABLE TO CSV::::::::
$table=$_REQUEST['t']; //table to export from
$col_names_query="SHOW COLUMNS FROM $table;";
$values_query="SELECT * FROM $table";
$fieldseparator=','; //excel needs comma rather than semi colon
$download_file=1; //should the file be downloaded from browser(1) or saved to the server(0)?
$filename = $table; //contains name of download file or (depending on $download_file) the path to save csv file without file extension.
exportCSV($values_query, $col_names_query, $col_count=NULL, $fieldseparator=',', $filename, $download_file);
die();
break;
case 'importmerge': //IMPORT CSV AND MERGE DUPLICATES::::::::
$import_to_table = $_REQUEST['t']; //existing table to import the CSV data to
$importfilename = $_FILES["uploadfile"]["tmp_name"];
$fieldseparator=','; //excel needs comma rather than semi colon
importCSV($importfilename, $fieldseparator, $import_to_table, $has_field_names=1, $merge_on_duplicate=1, $skip_first_col=0, $delete_after_import=1, $outputfile=0);
echo "<h3>IMPORT COMPLETE!</h3>";
break;
case 'exportblank'://EXPORT A BLANK FILE WITH CERTAIN COLUMN HEADINGS::::::::
$table=$_REQUEST['t']; //table to export from
$col_names_query="SHOW COLUMNS FROM $table WHERE field!='newsID' and field!='current';";
$values_query="SELECT NULL;";
$fieldseparator=','; //excel needs comma rather than semi colon
$download_file=1; //should the file be downloaded from browser(1) or saved to the server(0)?
$filename = $table; //contains name of download file or (depending on $download_file) the path to save csv file without file extension.
exportCSV($values_query, $col_names_query, $col_count=NULL, $fieldseparator=',', $filename, $download_file);
die();
break;
case 'importappend': //IMPORT CSV AND ADD RECORDS TO END OF TABLE::::::::
$import_to_table = $_REQUEST['t']; //existing table to import the CSV data to
$importfilename = $_FILES["uploadfile"]["tmp_name"];
$fieldseparator=','; //excel needs comma rather than semi colon
importCSV($importfilename, $fieldseparator, $import_to_table, $has_field_names=1, $merge_on_duplicate=0, $skip_first_col=0, $delete_after_import=1, $outputfile=0);
echo "<h3>IMPORT COMPLETE!</h3>";
break;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>mysql2csv Demos</title>
</head>
<body>
<!--///////////////////////// DEMO 1 ////////////////////////////////-->
<h2>Edit entire table:</h2>
<p>
<a href="demo.php?s=exportall&t=newsdemo">
» Edit table in Excel
</a>
</p>
<form id="importcsv" name="importcsv" method="post" action="demo.php" enctype="multipart/form-data">
Import Edited File:
<input name="uploadfile" type="file" id="uploadfile" />
<input name="s" type="hidden" value="importmerge" />
<input name="t" type="hidden" value="newsdemo" />
<input type="submit" name="uploadbtn" id="uploadbtn" value="Upload" />
</form>
<!--///////////////////////// DEMO 2 ////////////////////////////////-->
<h2>Append New Records:</h2>
<p>
<a href="demo.php?s=exportblank&t=newsdemo">
» Add records with Excel (excluding certain fields)
</a>
</p>
<form id="importcsv2" name="importcsv2" method="post" action="demo.php" enctype="multipart/form-data">
Import Edited File:
<input name="uploadfile" type="file" id="uploadfile" />
<input name="s" type="hidden" value="importappend" />
<input name="t" type="hidden" value="newsdemo" />
<input type="submit" name="uploadbtn2" id="uploadbtn2" value="Upload" />
</form>
<p> </p>
</body>
</html>