0

in the result(at excel file)i see html code i fact this is false i just user list.

i wrote this code at html:

<form action="/search.php" method="post" novalidate>
        <button type="submit" name="excel" class="btn btn-outline-primary">ایجاد فایل اکسل</button>
</form>

php:

$db = new PDO("mysql:host=localhost;dbname=pasak", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$select_stmt = $db->prepare("SELECT * FROM search  ");
$select_stmt->execute();

if(isset ($_POST['excel'])){
    if ($select_stmt) {
        $delimiter = ";";
        $filename = "users_" . date('Y-m-d') . ".csv";
        header('Content-Encoding: UTF-8');
        header('Content-type: application/csv; charset=UTF-8');
        header('Content-Disposition: attachment; filename=' . $filename);
        $f = fopen('php://output', 'w');
        fputs($f, "\xEF\xBB\xBF"); // UTF-8 BOM !!!!!
        $fields = ['شناسه', 'نام', 'نام خانوادگی', 'سن'];
        fputcsv($f, $fields, $delimiter);
        while ($row= $select_stmt->fetch(PDO::FETCH_ASSOC )) {
                $linedata = [$row['id'], $row['name'], $row['family'], $row['age']];
            fputcsv($f, $linedata, $delimiter);
        }
        fpassthru($f);
    }
}
Your Common Sense
  • 154,967
  • 38
  • 205
  • 325
mahnaz
  • 1
  • 2

0 Answers0