<?php
/**
* MiniClick
* Gestionnaire de tableau collaboratif
* Copyright (C) 2006 Guillaume Monet <gm@ibreizh.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
* @package miniclick
* @version 5.0.0
*/

//Test de l'appel par le main ou un descendant 
if($GLOBALS['main'] != 1){
	die("Cannot acces directly to this page");
}

function export_cvs_table($query){
	flush();
	//header("Content-Type: application/csv-tab-delimited-table");
	//header("Content-disposition: filename=table.csv");
	$myFile = SMARTY_CACHE_DIR."export".session_id().".csv";
	$fh = fopen($myFile, 'w');

	// Liste les données de la table
	// -------------------------------------------
	$resQuery = mysql_query($query);
	
	if (mysql_num_rows($resQuery) != 0) {
	// titre des colonnes
	$fields = mysql_num_fields($resQuery);
		$i = 0;
		while ($i < $fields) {
			fwrite($fh,mysql_field_name($resQuery, $i).";");
			$i++;
		}
		fwrite($fh,"\n");
	
		// données de la table
		while ($arrSelect = mysql_fetch_array($resQuery, MYSQL_ASSOC)) {
			foreach($arrSelect as $elem) {
				fwrite($fh,"$elem;");
			}
			fwrite($fh,"\n");
		}
	}
	fclose($fh);
	echo "L'export est pret <a href='cache/export".session_id().".csv'>Telecharger</a>";
}
?>
