3

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! enter image description here

what I'm trying to achieve is to have the error message looking like this: enter image description here

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 :! enter image description here

1

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

Yes, I just edited my answer so you can have a look if you have time to see where am I going wrong!
you are echoing the headers over and over again with each row - echo them OUTSIDE the foreach loop and inside the loop, echo only the rows with the error messages
And add <tr> before and </tr> before and after the header cells, and inside the foreach loop at the beginning and end, and erase them from the part where you open and close the table - you need them in every row!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.