Columns don't order by SQL SELECT statement on export to CSV

tidusx18 28 Apr, 2009
Hi,

Having a little problem...I followed the tutorial on how to export some data to a CSV file and it works great. The only things are that even though I use the column names that I want to export, they don't appear in that order in the CSV file. They appear in the same order as they are in the database table. Also, the column headers still show up even if I include their names in the SELECT statement.

Here is my code:
    <?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_Email_marketing_form';
       $tables = array( $tablename );
       $result = $database->getTableFields( $tables );
       $table_fields = array_keys($result[$tablename]);
       
       $database->setQuery( "SELECT Last_name, First_name 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();
    ?>


and this is the SELECT statement within the above code (just for your convenience😀 lol:
$database->setQuery( "SELECT Last_name, First_name FROM ".$tablename."" );


You can also use this link to the form to see the resulting CSV file...
http://cityflyers.net/joomla/index.php?option=com_chronocontact&chronoformname=Email_marketing_download

Thank you,

PS: I know I've been asking a lot of questions and I just want you guys to know that I am very appreciative of all the help I have received. Thanks again.

Daniel-
Max_admin 30 Apr, 2009
Hi Daniel,

it will appear the same as the order in the DB table but you can change the table fields array to have the order you need:

$table_fields = array_keys($result[$tablename]);


to

$table_fields = array('lastname', 'firstname', ....);


Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.