<?php
error_reporting(0);
ob_start();
session_start();
include_once("../include/dbconnect.php");
if($_GET['action']=="download"){
// Query to fetch data from your database table
$sql = "SELECT * FROM mec_all_products where mec_subid=".$_GET['mec_subid'];
$result = $conn->query($sql);

// Fetch data and store in array
$data = array();
$data[] = array("S.No","Product Name","Unit Type","Quantity","Price","wieght","Description");$i=1;
while ($row = $result->fetch_assoc()) {
    $data[] = array($i,stripslashes($row['pname']), stripslashes($row['unit_type']), $row['quantity'], $row['price'], $row['weight'], stripslashes($row['mec_description']));
    $i++;}

// Close the database connection
$conn->close();

// Output CSV file
$csvFileName = 'exported_data.csv';
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $csvFileName . '"');

// Open the output stream
$output = fopen('php://output', 'w');

// Write the data to the CSV file
foreach ($data as $row) {
    fputcsv($output, $row);
}

// Close the output stream
fclose($output);
}
    ?>