如何将Amazon MWS GetReport结算数据转为Bootstrap整洁表格
Turn Amazon GetReport Data into a Bootstrap Table
Got it, let's walk through how to turn your Amazon GetReport data into a properly formatted Bootstrap table—no hardcoded headers required. Here's how to adjust your existing code:
Step 1: Parse the Raw Report Content
Amazon's GetReport returns data in CSV or TSV format (most often CSV for settlement reports). First, we need to extract this raw content and convert it into a usable array structure.
Step 2: Dynamically Generate the Bootstrap Table
Instead of writing static <th> elements, we'll pull headers directly from the first row of the report, then loop through the data rows to populate the table.
Here's the updated, complete function:
function invokeGetReport(MarketplaceWebService_Interface $service, $request) { try { $response = $service->getReport($request); // Extract the raw report content (CSV/TSV text) $reportContent = $response->getReport(); // Handle empty report case if (empty(trim($reportContent))) { echo "<div class='alert alert-warning'>No data available in the settlement report.</div>"; return; } // Parse CSV into an array (adjust delimiter if using TSV) $rows = array_map('str_getcsv', explode("\n", trim($reportContent))); // Pull headers from the first row, then remove it from the data set $headers = array_shift($rows); // Start building the Bootstrap table echo "<table class='table table-bordered table-striped table-hover table-condensed table-responsive'>"; // Table Header echo "<thead><tr>"; foreach ($headers as $header) { // Clean up header text and escape special characters echo "<th>" . htmlspecialchars(trim($header)) . "</th>"; } echo "</tr></thead>"; // Table Body echo "<tbody>"; foreach ($rows as $row) { // Skip empty rows (common in report exports) if (empty(array_filter($row))) continue; echo "<tr>"; foreach ($row as $cell) { // Escape cell content to prevent XSS issues echo "<td>" . htmlspecialchars(trim($cell)) . "</td>"; } echo "</tr>"; } echo "</tbody></table>"; } catch (MarketplaceWebService_Exception $e) { // Show user-friendly error with Bootstrap alert styling echo "<div class='alert alert-danger'> Failed to fetch report: " . htmlspecialchars($e->getMessage()) . " </div>"; } }
Key Notes:
- Delimiter Adjustment: If your settlement report uses TSV (tab-separated values) instead of CSV, replace the parsing line with this:
$rows = array_map(function($line) { return str_getcsv($line, "\t"); // Use tab as delimiter }, explode("\n", trim($reportContent))); - XSS Protection: Using
htmlspecialchars()ensures any special characters in Amazon's data (like&,<, or") don't break your HTML or introduce security risks. - Empty Row Handling: The
array_filter()check skips any blank rows that might be included in the report export. - Responsive Styling: The
table-responsiveclass ensures the table scales properly on mobile devices.
内容的提问来源于stack exchange,提问作者Steven Smith
相关产品推荐
相关产品推荐

