0

I have a array that is dynamically generated and i am able to manipulate the data and print it in a table but I want to be able to print it differently.

This is what I have been able to do so far:

enter image description here

This is what I want to achieve

As you can see the array has multiple entries and for each entry I would like to create a new table and display the data like so:

enter image description here

There are some keys in the array I would prefer to leave out from being added to the table. the key names are: Id, CreatedDate, Incoming

Here is a short sample of what the array looks like:

[records] => Array
        (
            [0] => Array
                (
                    [Id] => 
                    [CreatedDate] => 2016-02-18T02:24:57.000Z
                    [FromName] => Technical Support
                    [Incoming] => 
                    [MessageDate] => 2016-02-18T02:24:57.000Z
                    [Subject] => this is a test reply
                    [TextBody] => testt ref:_00D708cJQ._50080oYTuD:ref
                    [ToAddress] => [email protected]
                )

            [1] => Array
                (
                    [Id] => 
                    [CreatedDate] => 2016-02-17T13:36:52.000Z
                    [FromName] => Technical Support
                    [Incoming] => 1
                    [MessageDate] => 2016-02-17T13:36:08.000Z
                    [Subject] => MySupport Portal: Test Email via API
                    [TextBody] => this is a test email 4 ref:_00D708cJQ._50080oYTuD:ref
                    [ToAddress] => [email protected]
                )

Here is my current php code

        $count = $response_array['size'] -1;

        //print table headers
        echo '<table border=1><tr>';
        foreach( $response_array['records'][0] as $key => $value ) {
            if($key !== 'Id') {
                echo '<th>'.$key.'</th>';
            }
        }
        echo '</tr><tr>';

        //print table data
        for ($i = 0; $i <= $count; $i++) {
            foreach( $response_array['records'][$i] as $key => $value ) {
                if($key !== 'Id') {
                    if($key === 'TextBody') {
                        echo '<td><pre>'.$value.'</pre></td>';
                    } else {
                        echo '<td>'.$value.'</td>';
                    }
                }
            }
            echo '</tr><tr>';
        }
        echo "</tr></table>";

I know how to write the HTML but not sure how to tie in the php as im not sure how i can sort the headers to be in different part of the table.. in any case here is the html with dummy data as placeholders

                <table border=1>
                <tr>
                    <th>MessageDate</th>
                    <th>FromName</th>
                    <th>ToAddress</th>
                </tr><tr>
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                </tr><tr>
                    <th colspan=3>Subject</th>
                </tr><tr>
                    <td colspan=3>this is the subject</td>
                </tr><tr>
                    <th colspan=3>TextBody</th>
                </tr><tr>
                    <td colspan=3>this is the body</td>
                </tr>
            </table>
4
  • 3
    What is the problem ? create the template you want for 1 line of the array, then loop through each line. Commented Feb 19, 2016 at 12:25
  • i know how to write the html but not sure how to tie in the php since the headers will be in different places Commented Feb 19, 2016 at 12:35
  • ive just updated my post with the html and included dummy data. would appreciate some help with the php Commented Feb 19, 2016 at 12:37
  • it seems like you do not even want to try this code to see if it works. You post here without testing, just in case it doesn't work Commented Feb 19, 2016 at 12:40

2 Answers 2

1

Okay, since you said you are able to do it for 1 line lets show you how to do it for all the line.

Just loop thourgh all the line like this.

<?php foreach( $response_array['records'] as $records): ?>
           <table border=1>
                <tr>
                    <th>MessageDate</th>
                    <th>FromName</th>
                    <th>ToAddress</th>
                </tr><tr>
                    <td><?php echo $records['MessageDate'] ?></td>
                    <td><?php echo $records['FromName'] ?></td>
                    <td><?php echo $records['ToAddress'] ?></td>
                </tr><tr>
                    <th colspan=3>Subject</th>
                </tr><tr>
                    <td colspan=3><?php echo $records['Subject'] ?></td>
                </tr><tr>
                    <th colspan=3>TextBody</th>
                </tr><tr>
                    <td colspan=3><?php echo $records['TextBody'] ?></td>
                </tr>
            </table>
<?php endforeach; ?>

edit: Added php tags

Sign up to request clarification or add additional context in comments.

2 Comments

thank you! that works as needed. didnt seem too difficult but i am able to understand what you you did there
That was the point! Hope this will help, you were pretty close actually
1

Unex's answer is perfect. Here is another alternative:-

echo '<table border=1>';
        foreach( $response_array['records'] as $key => $value ) {             

           if($key == 'MessageDate'){
              echo "<tr>";
              echo "<th>MessageDate</th>";
              echo "<th>FromName</th>";
              echo "<th>ToAddress</th>";
              echo "</tr>";
              echo "<tr>";
              echo "<td>{$response_array['records'][$key]['MessageDate']}</td>";
              echo "<td>{$response_array['records'][$key]['FromName']}</td>";
              echo "<td>{$response_array['records'][$key]['ToAddress']}</td>";
              echo "</tr>";
           }

           if($key == 'Subject'){
              echo '<tr>';
              echo "<th colspan='3'>Subject</th>";
              echo "</tr>";
              echo '<tr>';
              echo "<td colspan='3'>{$response_array['records'][$key]['Subject']}</td>";
              echo "</tr>";
           }


           if($key == 'TextBody'){
              echo "<tr>";
              echo "<th colspan='3'>TextBody</th>";
              echo "</tr>";
              echo "<tr>";
              echo "<td colspan='3'>{$response_array['records'][$key]['TextBody']}</td>";
              echo "</tr>";
           }

        }
echo "</table>";

1 Comment

thanks Ravi! this is a good alternative as well. it helped me learn something new. much appreciate your tie.

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.