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

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数