Export WordPress Table to CSV from page created in admin
-
Hey,
I have a problem I can not solve.
I have made a page called “Reports” in the admin menu, I have displayed the table with the fields in page, now I have a button and export the table to CSV format between 2 dates, I have data start and end data. I succeeded to display the table but the download will automatically appear and I don’t want this, I want to download it only when I press the button. I mention that all the code is written in functions.php. Here is my code :
function my_admin_menu() {
add_menu_page( ‘Reports’, ‘Reports’, ‘manage_options’, ‘reports.php’, ‘reports_admin_page’, ‘dashicons-tickets’, 6 );
}
function reports_admin_page(){ ?>Reports that will allows to create a report SF Requests
” method=”post”>
Start Date
End Date
Export to CSVexample 1
example 2
“;while($row = mysqli_fetch_array($result)) {
echo “”;
echo “” . $row[‘id’] . “</td>”;
echo “<td>” . $row[‘user’] . “</div></td>”;
echo “</tr>”;
}
echo “</table>”;mysqli_close($con);
?>
<?php
$database = “y”;
$con = mysql_connect(“x”,’z’,”, ‘y’) or die( “Unable to Connect database”);
mysql_select_db($database,$con) or die( “Unable to select database”);
//$date1 = $_POST[‘date1’];
//$date2 = $_POST[‘date2’];
// Table Name to export in csv
$ShowTable = “table”;$FileName = “reports_”.date(‘Ymd’) . “.csv”;
$file = fopen($FileName,”w”);
//$sql = mysql_query(“SELECT * FROM$ShowTableWHERE $start >= $date1 AND $end <= $date2″);
$sql = mysql_query(“SELECT * FROM$ShowTable“);
$row = mysql_fetch_assoc($sql);$HeadingsArray=array();
foreach($row as $name => $value){
$HeadingsArray[]=$name;
}
fputcsv($file,$HeadingsArray);while($row = mysql_fetch_assoc($sql)){
$valuesArray=array();
foreach($row as $name => $value){
$valuesArray[]=$value;
}
fputcsv($file,$valuesArray);
}
fclose($file);header(“Location: $FileName”);
?>
</div>
<?php
} -
Hey sorinavld,
Please can you mention the link to your website.
From your problem, it seems that you are using the a self-hosted WordPress website found on WordPress.org.
I am afraid you are on the wrong forums!
You could look in WordPress.org forum, perhaps someone there has experienced something similar: https://wordpress.org/support/
You’ll need a free WordPress.org account to post – if you don’t already have one, you can register here: https://wordpress.org/support/register.php
-
- The topic ‘Export WordPress Table to CSV from page created in admin’ is closed to new replies.