在php中生成带循环的动态表

时间:2018-01-29 13:25:28

标签: php html5

如果我问一个愚蠢的问题,请原谅我!但我真的很努力,但仍然失败 我试图创建一个使用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

2 个答案:

答案 0 :(得分:3)

您可以尝试以下操作,然后将样式类添加到其中。这应该创造你的桌子!

   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>

答案 1 :(得分:1)

您没有发布任何尝试过的内容,因此通常的做法是:

if - 条件内(如果错误),但在foreach循环之前,回显表的开头,包括标题行,如< / p>

echo "<table>
  <tr>
    <th>Header for Row 1<th>
    <th>Header for Row 2<th>
    <th>Header for Row 3<th>
  </tr>";

然后在foreach循环内首先回显<tr>标记,然后在每个列内容变量之前回显<td>,在每个列内容变量之后回合</td>并最终一个外观<tr>

foreach循环之后(但仍在if条件内),回显结束</table>标记。