Quantcast
Channel: Topic Tag: MySQL | WordPress.org
Viewing all articles
Browse latest Browse all 5540

Export WordPress Table to CSV from page created in admin

$
0
0

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 CSV

example 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 $ShowTable WHERE $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
}

Viewing all articles
Browse latest Browse all 5540

Trending Articles