首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用PHP stdClass对象创建spec HTML表

用PHP stdClass对象创建spec HTML表
EN

Stack Overflow用户
提问于 2018-07-11 05:44:28
回答 1查看 437关注 0票数 1

我有一个日期数组,如下所示

代码语言:javascript
复制
Array
(
    [0] => stdClass Object
      (
        [question_id] => 64
        [title] => Question1
        [question_type_id] => 1
        [settings] => 
        [poll_id] => 5
        [answer] => Array
            (
                [0] => stdClass Object
                    (
                        [answer_id] => 1
                        [poll_id] => 5
                        [question_id] => 64
                        [answer] => Answer1-1
                        [created_at] => 2018-07-08 17:36:15
                    )

                [1] => stdClass Object
                    (
                        [answer_id] => 5
                        [poll_id] => 5
                        [question_id] => 64
                        [answer] => Answer1-2
                        [created_at] => 2018-07-08 19:27:33
                    )

            )

    )

[1] => stdClass Object
    (
        [question_id] => 65
        [title] => Question2
        [question_type_id] => 6
        [settings] => ["više od 2km","manje od 2km"]
        [poll_id] => 5
        [answer] => Array
            (
                [0] => stdClass Object
                    (
                        [answer_id] => 2
                        [poll_id] => 5
                        [question_id] => 65
                        [answer] => Answer2-1
                        [created_at] => 2018-07-08 17:36:47
                    )

                [1] => stdClass Object
                    (
                        [answer_id] => 6
                        [poll_id] => 5
                        [question_id] => 65
                        [answer] => Answer2-2
                        [created_at] => 2018-07-09 23:31:31
                    )

            )

    )

我需要一个在浏览器中的表格,看起来像在这里输入图像描述

代码语言:javascript
复制
Question1   Question2   Question3
Answer1-1   Answer2-1   Answer3-1
Answer1-2   Answer2-2   Answer3-2

这是我的代码:

代码语言:javascript
复制
<table class="table">
    <thead>
        <tr>
        <?php foreach ($DATA['question'] as $question): ?>
        <th><?php echo htmlspecialchars($question->title); ?></th>
        <?php endforeach; ?>
        </tr>
    </thead>
    <tbody>
       <tr>
        <?php foreach ($DATA['question'] as $question): ?>
        <td><?php for ($i=0,$j=count((array)$question->answer);$i<$j;$i++) {
            echo htmlspecialchars($question->answer[$i]->answer);
            }?></td>
        <?php endforeach; ?>
        </tr>
    </tbody>

我的问题是我不能把Answer1-2放在表的下一行。有什么想法吗?非常感谢

EN

回答 1

Stack Overflow用户

发布于 2018-07-12 02:20:11

非常感谢你的回答!这是我的解决方案。

代码语言:javascript
复制
<table class="table">
    <thead>
        <tr>
        <?php foreach ($DATA['question'] as $question): ?>
        <th><?php echo htmlspecialchars($question->title); ?></th>
        <?php endforeach; ?>
        </tr>
    </thead>
    <tbody>
       <tr>
        <?php foreach ($DATA['question'] as $question): ?>
        <td><?php for ($i=0,$j=count((array)$question->answer);$i<$j;$i++) {
            echo htmlspecialchars($question->answer[$i]->answer);
            echo "<br />";
            }?></td>
        <?php endforeach; ?>
        </tr>
    </tbody>
</table>                

以及它看起来是什么样子。

enter image description here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51274315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档