在PHP中使用循环生成动态表

如果我要问一个愚蠢的问题,请原谅我!但我真的很努力,但仍然失败
我正在尝试创建一条错误消息,该错误消息将使用PHP出现在循环中
原始代码如下所示:

    // go through lines that had errors
    if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
        foreach ($result->return->failedRows as $failedRow) {
            foreach ($failedRow->errors as $rowErrors)
                $message .= "\nline: " . ($failedRow->line) . ", column: " . $rowErrors->column . ", value: '" . $rowErrors->value . "'" . ", message: '" . $rowErrors->details[0]->translated . "'";
        }
    }
    if ($message != "")
        throw new Exception('Error details: ' . $message . ' [' . $method . ']');

错误消息现在看起来像这样!

enter image description here

我要实现的是使错误消息看起来像这样:

enter image description here

再次抱歉,如果这对您中的每个人来说似乎都很简单,但是我的PHP技能却很有限,我正在尝试学习所有这些看起来很容易创建的技巧!

好的,这是我尝试过的方法,但这当然是错误的!

// go through lines that had errors
if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
    foreach ($result->return->failedRows as $failedRow) {
        foreach ($failedRow->errors as $rowErrors)
            $message .= "<th>Line</th>" . 
                            "<td>" . ($failedRow->line) . "</td>" . 
                        "<th>Column</th>" . 
                            "<td>".$rowErrors->column. "</td>". 
                        "<th>Value</th>" . 
                            "<td>".$rowErrors->value . "</td>" . 
                        "<th>Error message</th>" . 
                            "<td>".$rowErrors->details[0]->translated."</td>";
    }
}
if (property_exists($result, 'errors') && is_object($result->errors) && count($result->errors) > 0) {
    foreach ($result->errors as $error)
        $message .= "\nerror: " . $error;
}

if ($message != "")
    echo "<div class='container'><strong>Error details:</strong></br>
            <table style='width:100%;'>
                <tr>            
                        $message
                </tr>
            </table></div>;

这是我苦苦尝试的结果:!

enter image description here

解决方法:

您可以尝试以下方法,然后向其中添加样式类.那应该创建您的表!

   if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
        foreach ($result->return->failedRows as $failedRow) {
            foreach ($failedRow->errors as $rowErrors)
                $message .= "<tr>" .
                                "<td>" . ($failedRow->line) . "</td>" . 
                                "<td>".$rowErrors->column. "</td>". 
                                "<td>".$rowErrors->value . "</td>" . 
                                "<td>".$rowErrors->details[0]->translated."</td>" .
                            "</tr>";
        }
    }

    if ($message != "")
        echo "<div class='container'><strong>Error details:</strong></br>
                <table id='t01' style='width:100%;'>
                    <tr>
                        <th>Line</th>
                        <th>Column</th>
                        <th>Value</th>
                        <th>Error message</th>
                            $message
                    </tr>
                </table></div>";

您还可以在前端添加以下CSS:

<style>

table {
    width:100%;
}
table, th, td {
    border: 1px solid #fff;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
    text-align: left;
}
table#t01 tr:nth-child(even) {
    background-color: #eee;
}
table#t01 tr:nth-child(odd) {
   background-color:#fff;
}
table#t01 th {
    background-color: #ED7D31;
    color: white;
}
</style>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。