如何使用PHP导出下拉筛选器选中的报告数据为CSV?
Alright, let's figure out how to export only the filtered data from your product seller report to CSV using PHP. I'll walk you through adapting your existing code to make this work smoothly:
First, we'll modify your existing productsellerreportAction to capture the selected filter values and add a dedicated function to generate the CSV file. Here's the step-by-step implementation:
Step 1: Capture Filter Parameters & Add Export Trigger
Update your action method to grab the selected dropdown value (e.g., store ID) and handle the CSV export request:
public function productsellerreportAction(){ global $mySession ; $this->view->pageHeading = "Product seller"; $this->view->pageDescription = "Booking Report"; // Get the selected store ID from your dropdown filter $selectedStoreId = $this->_getParam('selected_store_id', null); // Fetch store data based on admin permissions (your existing logic) if($this->adminLogged->admin_id == 1){ $storeData = $this->modelSuper->Super_Get('stores'," 1","fetchAll"); } else { $storeData = $this->modelSuper->Super_Get('stores'," store_id = ".$this->adminLogged->store_id,"fetchAll"); } // Trigger CSV export if the export button is clicked if($this->_getParam('export_csv', false)){ $this->exportFilteredReportToCSV($selectedStoreId); exit; // Halt execution after export to avoid rendering the page } // Rest of your existing code to display the filtered report... $this->view->storeData = $storeData; // ... add logic to fetch and pass filtered report data to the view }
Step 2: Build the CSV Export Function
Add a private method to fetch the filtered data, format it as CSV, and trigger a download for the user:
private function exportFilteredReportToCSV($selectedStoreId){ // Build the WHERE clause based on the selected filter and admin permissions $whereCondition = ""; // Add selected store filter if present if($selectedStoreId){ // Sanitize the ID to prevent SQL injection (critical!) $safeStoreId = (int)$selectedStoreId; $whereCondition = " store_id = {$safeStoreId}"; } // Enforce non-admin store restriction if($this->adminLogged->admin_id != 1){ $adminStoreId = (int)$this->adminLogged->store_id; $whereCondition = $whereCondition ? "{$whereCondition} AND " : ""; $whereCondition .= " store_id = {$adminStoreId}"; } // Fetch the filtered report data from your database table $reportData = $this->modelSuper->Super_Get('your_report_table_name', $whereCondition, "fetchAll"); // Set HTTP headers to trigger CSV download header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=product_seller_report_'.date('Y-m-d').'.csv'); // Open output stream to write CSV content $output = fopen('php://output', 'w'); // Write CSV header row (customize these columns to match your report) fputcsv($output, ['Product Name', 'Seller Name', 'Total Bookings', 'Store Location']); // Write each filtered data row to the CSV foreach($reportData as $row){ fputcsv($output, [ $row['product_name'], $row['seller_name'], $row['total_bookings'], $row['store_location'] ]); } fclose($output); }
Step 3: Add Export Button to Your View
Update your report's HTML template to include an export button that submits the selected filter values:
<form method="post" action="<?php echo $this->url(array('controller'=>'your_controller','action'=>'productsellerreport')); ?>"> <!-- Your store dropdown filter --> <select name="selected_store_id"> <option value="">All Stores</option> <?php foreach($this->storeData as $store): ?> <option value="<?php echo $store['store_id']; ?>"><?php echo $store['store_name']; ?></option> <?php endforeach; ?> </select> <!-- Buttons to show report or export filtered data --> <button type="submit">Show Filtered Report</button> <button type="submit" name="export_csv" value="1">Export to CSV</button> </form>
Important Notes:
- SQL Injection Protection: We cast the store IDs to integers to sanitize input—make sure your
Super_Getmethod also handles safe queries, or extend this sanitization as needed. - Customization: Adjust the CSV header rows and data mapping to match the actual columns in your report database table.
- Permission Enforcement: The export function respects your admin/non-admin access rules, so non-admins can only export data from their assigned store.
内容的提问来源于stack exchange,提问作者Vikas Saraswat

