【问题标题】:Create spec HTML Table using PHP stdClass Object使用 PHP stdClass 对象创建规范 HTML 表
【发布时间】:2018-12-18 20:03:34
【问题描述】:

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

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
                    )

            )

    )

我需要一个表格在浏览器中看起来像 在此处输入图片说明

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

这是我的代码:

<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 放在表格的下一行。 任何想法? 非常感谢

【问题讨论】:

    标签: php html arrays oop


    【解决方案1】:

    有几种方法可以做到这一点。

    • 将每个预期的列生成为单个元素并将其放置在单元格中。
    • 以您希望的显示方式填充数组
    • 创建一个 for(i=0;i&lt;maxAmountOfAnswers;i++) 包装另一个 foreach(问题)并逐行填充表格。
    • 使用 DataTables 或其他可以读取 json 的库。

    想想你为什么想要这个;语义表旨在显示一种数据。您是否有充分的理由不只为每个问题显示 1 个表格,或者根本不显示表格?

    【讨论】:

      【解决方案2】:

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

      <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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        • 1970-01-01
        • 1970-01-01
        • 2013-10-12
        • 2021-01-19
        • 1970-01-01
        相关资源
        最近更新 更多