简体   繁体   中英

Generate dynamic table with loop in php

Please forgive me if I'm asking a stupid question! But I really tried hard and still failing I was trying to create an error message that would appear in a loop using PHP The original code looks like this:

    // 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 . ']');

The error message looks like this right now! 在此输入图像描述

what I'm trying to achieve is to have the error message looking like this: 在此输入图像描述

Again sorry if this seems very simple for any of you but my PHP skills are rather limited and I'm trying to learn all these tricks that may seem very simple to create!

ok here's what I've tried but it's wrong of course!

// 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>;

and here's the result of my misurable try :! 在此输入图像描述

You can try the following and then add your styling classes to it. That should create your table!

   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>";

You can also add the following CSS in the front end:

<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>

You didn't post anything you tried, so the general way to do it is:

Inside the if -condition (if there are errors), but before the foreach loop, echo the beginning of the table, including the header row, like

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

Then inside the foreach loop, echo a <tr> tag first, then before every column content variable a <td> , after every column content variable a closing </td> and eventually a cloasing <tr>

After the foreach loop (but still inside the if condition), echo the closing </table> tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM