File manager - Edit - /var/www/order.cmtc.ac.th/admin/orders_export.php
Back
<?php session_start(); if(!isset($_SESSION['admin'])) { header('Location: index.php'); exit; } include('../config/db.php'); date_default_timezone_set('Asia/Bangkok'); // รับค่าจาก URL $date_from = $_GET['date_from'] ?? date('Y-m-d', strtotime('-6 days')); $date_to = $_GET['date_to'] ?? date('Y-m-d'); $filter_receive = $_GET['receive'] ?? 'all'; $search = trim($_GET['search'] ?? ''); // ✅ เงื่อนไขเฉพาะ order_type = ปกติ $where = "WHERE o.order_type='ปกติ' AND DATE(o.created_at) BETWEEN ? AND ?"; $params = [$date_from, $date_to]; $types = 'ss'; // วิธีรับสินค้า if($filter_receive != 'all'){ $where .= " AND o.receive_method=?"; $params[] = ($filter_receive == 'self' ? 'รับสินค้าด้วยตนเอง' : 'จัดส่งทางไปรษณีย์'); $types .= 's'; } // การค้นหา if($search != ''){ $where .= " AND (o.booking_no LIKE ? OR o.fullname LIKE ? OR o.phone LIKE ?)"; $s = "%".$search."%"; $params[] = $s; $params[] = $s; $params[] = $s; $types .= 'sss'; } // ✅ ดึงข้อมูลจากฐานข้อมูล $sql = " SELECT o.*, GROUP_CONCAT(CONCAT(p.name,' x',i.qty) SEPARATOR ', ') AS items FROM orders o JOIN order_items i ON o.id = i.order_id JOIN products p ON i.product_id = p.id $where GROUP BY o.id ORDER BY o.id DESC "; $stmt = $conn->prepare($sql); $stmt->bind_param($types, ...$params); $stmt->execute(); $res = $stmt->get_result(); // ✅ ตั้งค่าหัวไฟล์ให้เป็น Excel $filename = "orders_normal_" . date('Ymd_His') . ".xls"; header("Content-Type: application/vnd.ms-excel; charset=utf-8"); header("Content-Disposition: attachment; filename=\"$filename\""); header("Pragma: no-cache"); header("Expires: 0"); // ✅ สร้างตาราง Excel echo "<meta charset='UTF-8'>"; echo "<table border='1'>"; echo "<thead> <tr style='background:#eee;'> <th>ลำดับ</th> <th>เลขที่ใบจอง</th> <th>ชื่อผู้จอง</th> <th>เบอร์โทร</th> <th>รายการเหรียญ</th> <th>ยอดรวม (บาท)</th> <th>สถานะ</th> <th>วิธีรับสินค้า</th> <th>วันที่จอง</th> </tr> </thead><tbody>"; $no = 1; $sum_total = 0; while($row = $res->fetch_assoc()){ $sum_total += $row['total']; echo "<tr>"; echo "<td>".$no++."</td>"; echo "<td>".htmlspecialchars($row['booking_no'])."</td>"; echo "<td>".htmlspecialchars($row['fullname'])."</td>"; echo "<td>".htmlspecialchars($row['phone'])."</td>"; echo "<td>".htmlspecialchars($row['items'])."</td>"; echo "<td>".number_format($row['total'],2)."</td>"; echo "<td>".htmlspecialchars($row['status'])."</td>"; echo "<td>".htmlspecialchars($row['receive_method'])."</td>"; echo "<td>".htmlspecialchars($row['created_at'])."</td>"; echo "</tr>"; } // ✅ รวมยอดทั้งหมด echo "<tr style='font-weight:bold;background:#f9f9f9;'> <td colspan='5' align='right'>รวมทั้งหมด</td> <td>".number_format($sum_total,2)."</td> <td colspan='3'></td> </tr>"; echo "</tbody></table>"; exit; ?>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.4 |
proxy
|
phpinfo
|
Settings