We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4310
    • 2,310 Posts
    I have an export to CSV working as a snippet on a hide from menu, unpublished page.
    It works fine, exporting the web user data as a CSV.
    It would be nice to have it as a module.
    I’ve tried several attempts but all I get is a "<" in the first field of the CSV file.
    Can anyone see where I’m going wrong?
    Snippet:
    <?php
    $table = "modx_web_user_attributes";
    exportMysqlToCsv($table);
    function exportMysqlToCsv($table,$filename = 'export.csv')
    {
        $csv_terminated = "\n";
        $csv_separator = ",";
        $csv_enclosed = '"';
        $csv_escaped = "\\";
        $sql_query = "select * from $table";
    
        // Gets the data from the database
        $result = mysql_query($sql_query);
        $fields_cnt = mysql_num_fields($result);
    
    
        $schema_insert = '';
    
        for ($i = 0; $i < $fields_cnt; $i++)
        {
            $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
                stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
            $schema_insert .= $l;
            $schema_insert .= $csv_separator;
        } // end for
       
        $out = trim(substr($schema_insert, 0, -1));
        $out .= $csv_terminated;
    
        // Format the data
        while ($row = mysql_fetch_array($result))
        {
            $schema_insert = '';
            for ($j = 0; $j < $fields_cnt; $j++)
            {
                if ($row[$j] == '0' || $row[$j] != '')
                {
    
                    if ($csv_enclosed == '')
                    {
                        $schema_insert .= $row[$j];
                    } else
                    {
                        $schema_insert .= $csv_enclosed .
                        str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                    }
                } else
                {
                    $schema_insert .= '';
                }
    
                if ($j < $fields_cnt - 1)
                {
                    $schema_insert .= $csv_separator;
                }
            } // end for
    
            $out .= $schema_insert;
            $out .= $csv_terminated;
        } // end while
    
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Length: " . strlen($out));
        // Output to browser with appropriate mime type, you choose ;)
        header("Content-type: text/x-csv");
        //header("Content-type: text/csv");
        //header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=$filename");
        echo $out;
        exit;
    
    }
    ?>

    I’ve removed the PHP tags for the Module, tried the main code as an include added "global $modx;" etc. but all to no avail.
      • 10449
      • 956 Posts
      You’re creating a content-type header, and modx/php is probably already defining its own header + content-type, which most likely is text/html.
      Try to link to the file. I’ve done something similar recently (download a custom DB table as MS Excel) and that worked fine.
      Also, I would just put the file via FTP on the server, and use only an include() command in the module window.