以特殊方式从php数组中打印html表

时间:2016-02-19 12:21:19

标签: php

我有一个动态生成的数组,我能够操作数据并将其打印在表中,但我希望能够以不同的方式打印它。

这是我迄今为止所做的事情:

enter image description here

这就是我想要实现的目标

正如您所看到的,数组有多个条目, 为每个条目我想创建一个新表 并显示如下数据:

enter image description here

我希望数组中有一些键不会被添加到表中。关键名称是:Id,CreatedDate,Incoming

以下是数组的简短示例:

[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] => outa@gmail.com
                )

            [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] => outa@gmail.com
                )

这是我目前的PHP代码

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

我知道如何编写HTML但不知道如何绑定php,因为我不知道如何将标题排序到表的不同部分..无论如何这里是带有虚拟数据的html占位符

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

2 个答案:

答案 0 :(得分:1)

好的,既然你说你能够为1行做到这一点,那就告诉你如何为所有这一行做这件事。

就像这样循环所有线路。

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

编辑:添加了php标签

答案 1 :(得分:1)

Unex's answer很完美。这是另一种选择: -

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