Back to top

Autore Topic: RISOLTO:[Chronoforms] Export tabella in csv  (Letto 2891 volte)

Offline ZioRazio

  • Nuovo arrivato
  • *
  • Post: 7
    • Mostra profilo
RISOLTO:[Chronoforms] Export tabella in csv
« il: 02 Mar 2011, 12:10:18 »
Ciao a tutti ho bisogno di un'ennesimo aiutino.
Ho trovato sul forum della chronoengin lo script per esportare in csv una tabella solo che, per come è strutturato il programma, all'atto del submit mi chiede di salvare o di aprire il file.
Questo per il mio scopo non è applicabile perché vorrei che il file fosse scritto su una cartella sul server in modo che poi uno  scritp automatizzato lo prelevi e tramite ftp lo depositi dentro un'altro server.
Qualcuno può aiutarmi? intanto vi allego lo script originale e vi ringrazio in anticipo per l'aiuto che vorrete darmi.
Codice: [Seleziona]
    <?php
    
global $mainframe;
       
$database =& JFactory::getDBO();

       include_once 
JPATH_BASE.'/components/com_chronocontact/excelwriter/'."Writer.php";
       
//echo $_POST['formid'];
       /*$formid = JRequest::getVar( 'formid', array(), 'post', 'array');
       $database->setQuery( "SELECT name FROM #__chrono_contact WHERE id='".$formid[0]."'" );
       $formname = $database->loadResult();*/
       
       
$tablename 'jos_chronoforms_form_madre';
       
$tables = array( $tablename );
       
$result $database->getTableFields$tables );
       
$table_fields array_keys($result[$tablename]);
       
       
$database->setQuery"SELECT * FROM ".$tablename."" );
       
$datarows $database->loadObjectList();
       
       
$titcol 0;
       foreach(
$table_fields as $table_field){
          if(
$titcol){$csvline .=",";}
          
$csvline .= $table_field;
          
$titcol++;
       }
       
$csvline .="\n";
             
       
$datacol 0;
       
$rowcount 1;
       foreach(
$datarows as $datarow){
          foreach(
$table_fields as $table_field){
             if(
$datacol){$csvline .=",";}
             
$csvline .= '"'.addslashes($datarow->$table_field).'"';
             
$datacol++;
          }
          
$csvline .="\n";
          
$datacol 0;
          
$rowcount++;
       }
       
       if (
ereg('Opera(/| )([0-9].[0-9]{1,2})'$_SERVER['HTTP_USER_AGENT'])) {
          
$UserBrowser "Opera";
       }
       elseif (
ereg('MSIE ([0-9].[0-9]{1,2})'$_SERVER['HTTP_USER_AGENT'])) {
          
$UserBrowser "IE";
       } else {
          
$UserBrowser '';
       }
       
$mime_type = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ? 'application/octetstream' 'application/octet-stream';
       @
ob_end_clean();
       
ob_start();

       
header('Content-Type: ' $mime_type);
       
header('Expires: ' gmdate('D, d M Y H:i:s') . ' GMT');

       if (
$UserBrowser == 'IE') {
          
header('Content-Disposition: inline; filename="' "ChronoForms - ".$tablename." - ".date("j_n_Y").'.csv"');
          
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
          
header('Pragma: public');
       }
       else {
          
header('Content-Disposition: attachment; filename="' "ChronoForms - ".$tablename." - ".date("j_n_Y").'.csv"');
          
header('Pragma: no-cache');
       }
       print 
$csvline;
exit();
?>
« Ultima modifica: 04 Mar 2011, 12:38:10 da ZioRazio »

Offline ZioRazio

  • Nuovo arrivato
  • *
  • Post: 7
    • Mostra profilo
Re:[Chronoforms] Export tabella in csv
« Risposta #1 il: 04 Mar 2011, 12:37:48 »
Ciao alla fine ho trovato la soluzione. Ve la scrivo qui in caso qualcuno abbia i miei stessi problemi :)
Codice: [Seleziona]
    <?php
       $db 
mysql_connect ('host_db''db_user''password_db') or
          die (
'Unable to connect');
       
mysql_select_db('database'$db) or die(mysql_error($db));
            
$user=& JFactory::getUser();
       
//Creates the variables
       
$tablename jos_chronoforms_form_madre;
       
$filename '/' $tablename date("Y-m-d-H-i-s") . '.txt'//Used / at beginning of the file to tell the application to write it in the root
       //Query for saving the table
       
$query "SELECT * FROM $tablename WHERE cf_user_id=('".$user->id."') INTO OUTFILE '$filename'";
       
$result mysql_query($query);
    
?>


Livio :)

 



Web Design Bolzano Kreatif