On my website, I create a table from mysql data, and then now I want to add the buttom export button of the table so that the user can load the data as a CSV file.
For this, I wrote a dummy form:
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post"> <input type="submit" name="submit" value="Click Me"> </form>
And at the top of the php file I have:
if(isset($_POST['submit'])) { export(); }
In my export function, I have mysql stuff, I create an array and insert data into it, and then:
$header = array('name', 'date', 'total', 'success', 'opens', 'clicks', 'success_rate', 'open_rate', 'CTO', 'CTR') $fp = fopen('exportme2.csv', 'w'); fputcsv($fp, $header); foreach ($data as $lines) { fputcsv($fp, $lines); } fclose($fp);
After clicking the export button, I will have the exportme2.csv file, but it is empty! The data may be incorrect, so there will be nothing but, at a minimum, I should have the names of the headers.
Can you help me in this matter?
Thanks.