You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP循环生成动态表格式错误提示:代码修正求助

Fixing the Table-Based Error Display

Hey there! No need to apologize for asking questions—we all start somewhere, and getting error display right is super helpful for debugging. Let's fix your table output issue.

What Was Wrong With Your Original Attempt?

Your code was repeating header cells (<th>) for every single error entry and shoving all content into one table row (<tr>). That's why the table looked broken: tables need one set of headers at the top, then a separate row for each individual error.

Corrected Code

Here's the revised version that builds a proper, clean table:

<?php
$message = '';

// Handle failed rows with detailed line/column errors
if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
    // Add the table header once at the start
    $message .= '<tr>
                    <th>Line</th>
                    <th>Column</th>
                    <th>Value</th>
                    <th>Error Message</th>
                </tr>';

    foreach ($result->return->failedRows as $failedRow) {
        foreach ($failedRow->errors as $rowErrors) {
            // Create a new table row for each error entry
            $message .= '<tr>
                            <td>' . htmlspecialchars($failedRow->line) . '</td>
                            <td>' . htmlspecialchars($rowErrors->column) . '</td>
                            <td>' . htmlspecialchars($rowErrors->value) . '</td>
                            <td>' . htmlspecialchars($rowErrors->details[0]->translated) . '</td>
                        </tr>';
        }
    }
}

// Handle top-level general errors (if any)
if (property_exists($result, 'errors') && is_object($result->errors) && count($result->errors) > 0) {
    // If we already have detailed error rows, add a separator header
    if (!empty($message)) {
        $message .= '<tr><td colspan="4" style="padding: 10px 0; font-weight: bold; border-top: 2px solid #ccc;">Additional General Errors:</td></tr>';
    } else {
        // If no detailed errors, start with a simple header for general errors
        $message .= '<tr><th>Error Message</th></tr>';
    }

    foreach ($result->errors as $error) {
        $message .= '<tr><td colspan="4">' . htmlspecialchars($error) . '</td></tr>';
    }
}

// Only render the container and table if there are errors to show
if (!empty($message)) {
    echo '<div class="container">
            <strong>Error details:</strong><br>
            <table style="width:100%; border-collapse: collapse; margin-top: 10px;">
                ' . $message . '
            </table>
          </div>';
}
?>

Key Improvements:

  • Proper Table Structure: We define the header row once, then add a new <tr> for every error entry—this matches how HTML tables are supposed to work.
  • XSS Protection: Used htmlspecialchars() to escape all dynamic content. This prevents malicious characters from breaking your HTML or causing security issues.
  • Unified Formatting: Integrated both detailed row errors and top-level general errors into the same table for a consistent look, with a visual separator if both types exist.
  • Cleaner Logic: Separated table initialization from error row generation, making the code easier to read and tweak later.

Optional Styling Tip

To make the table look more polished, add some basic CSS (either inline or in your stylesheet):

table {
    border: 1px solid #ddd;
}
th, td {
    padding: 8px;
    text-align: left;
    border-bottom: 1px solid #ddd;
}
th {
    background-color: #f2f2f2;
}

内容的提问来源于stack exchange,提问作者Fabian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:55:01