dongzhan1948 2018-07-10 21:44
浏览 81
已采纳

使用PHP stdClass Object创建规范HTML表

I have an array of date, something like below

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
                    )

            )

    )

I need a table in browser to look like enter image description here

Question1   Question2   Question3
Answer1-1   Answer2-1   Answer3-1
Answer1-2   Answer2-2   Answer3-2

This is my code:

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

My problem is that I can not place Answer1-2 in the next row of table. Any idea? Thanks a lot

  • 写回答

2条回答 默认 最新

  • dsilhx5830 2018-07-10 21:54
    关注

    There are a few options how you can do this.

    • Generate each of your intended columns as a single element and place that in a cell.
    • Populate an array in the way you wish to display them
    • Create a for(i=0;i<maxAmountOfAnswers;i++) wrapping another foreach (questions) and populate the table row by row.
    • Use DataTables or other libraries that can read a json.

    Think about why you want this though; semantically tables are intended to display one kind of data. Is there a good reason why you wouldn't just show 1 table per question, or tables at all?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致