【问题标题】:Looping an array to create a table, table and table rows not getting closed循环数组以创建表,表和表行未关闭
【发布时间】:2012-01-10 17:14:30
【问题描述】:

我目前正在处理一个项目,这需要我创建一个 HTML 视图,然后将其发送到 DOMPDF 以创建该视图的 PDF。

但是,构建我的视图的循环编写得不是很好,并且在创建表和行时,关于打开的 HTML 标记是否实际关闭似乎零一致性,这导致了我的 DOMPDF 问题因为它不喜欢没有正确关闭的表格或 HTML 元素,所以下面是我正在使用的数组以及 PHP 循环。

数组

    Array
(
    [0] => Array
        (
            [credit_id] => 11
            [credit_heading] => Testing another new entry
            [credit_title] => Testing another new entry
            [credit_role] => Testing another new entry
            [credit_director] => Testing another new entry
            [credit_type] => long
            [credit_position] => 0
            [date_created] => 2012-01-10 17:06:24
            [candidates_candidate_id] => 54
            [rel_id] => 8
            [credits_candidate_id] => 54
            [credits_credit_id] => 11
            [category_title] => Feature Film
            [category_position] => 0
        )

    [1] => Array
        (
            [credit_id] => 1
            [credit_heading] => Test Heading 1
            [credit_title] => Test Title 1
            [credit_role] => Test Role 1
            [credit_director] => Test Director 1
            [credit_type] => long
            [credit_position] => 1
            [date_created] => 2012-01-06 17:23:41
            [candidates_candidate_id] => 54
            [rel_id] => 1
            [credits_candidate_id] => 54
            [credits_credit_id] => 1
            [category_title] => Corporate
            [category_position] => 3
        )

    [2] => Array
        (
            [credit_id] => 6
            [credit_heading] => Test Heading 4
            [credit_title] => Test Title 4
            [credit_role] => Test Role 4
            [credit_director] => Test Director 4
            [credit_type] => long
            [credit_position] => 2
            [date_created] => 2012-01-06 17:20:40
            [candidates_candidate_id] => 54
            [rel_id] => 3
            [credits_candidate_id] => 54
            [credits_credit_id] => 6
            [category_title] => Corporate
            [category_position] => 3
        )

)

代码

<?php if($credit['credit_type'] == "long") : ?>
            <?php if($credit['category_title'] != $oldvalue) : ?>
                <table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;" class="long-entry">
                <tbody>
            <?php endif; ?> 
                    <?php if($credit['category_title'] != $oldvalue) : ?>
                    <tr>
                          <td width="25%"><strong><?php echo trim($credit['category_title']); ?></strong></td>                 
                          <td width="25%"><strong>Title</strong></td>
                          <td width="25%"><strong>Role</strong></td>
                          <td width="25%"><strong>Director</strong></td>
                    </tr>
                    <?php else : ?>
                    <tr>
                        <td width="25%"><?php echo $credit['credit_heading'];?></td>
                        <td width="25%"><?php echo $credit['credit_title']; ?></td>
                        <td width="25%"><?php echo $credit['credit_role']; ?></td>
                        <td width="25%"><?php echo $credit['credit_director']; ?></td>
                    </tr>
                    <?php endif; ?>
                          </tbody>
                          </table>
                <?php $oldvalue = $credit['category_title']; ?>
        <?php endif; ?> 

上面的代码试图实现的是,虽然 $credit['category_title'] 是新的,但我们创建了一个新表,如果在下一次迭代中它仍然相同,我们将一行添加到表中,依此类推向前。实际上是它创建了一个新表并添加了一个新行,然后不关闭该行或该表。

预期结果

<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
  <tr>
    <td width="10%"><strong>Corporate</strong></td>

    <td width="40%"><strong>Title</strong></td>

    <td width="25%"><strong>Role</strong></td>

    <td width="25%"><strong>Director</strong></td>
  </tr>

  <tr>
    <td width="10%">Test Heading 1</td>

    <td width="40%">Test Title 1</td>

    <td width="25%">Test Role 1</td>

    <td width="25%">Test Director 1</td>
  </tr>

  <tr>
    <td></td>
  </tr>

  <tr>
    <td width="10%">Test Heading 4</td>

    <td width="40%">Test Title 4</td>

    <td width="25%">Test Role 4</td>

    <td width="25%">Test Director 4</td>
  </tr>
</tbody>

实际结果

<table width="100%" cellspacing="0" cellpadding="0" border="0">
    <tbody>
      <tr>
        <td width="10%"><strong>Corporate</strong></td>

        <td width="40%"><strong>Title</strong></td>

        <td width="25%"><strong>Role</strong></td>

        <td width="25%"><strong>Director</strong></td>
      </tr>

      <tr>
        <td width="10%">Test Heading 1</td>

        <td width="40%">Test Title 1</td>

        <td width="25%">Test Role 1</td>

        <td width="25%">Test Director 1</td>
      </tr>

      <tr>
        <td></td>
      </tr>
      Test Title 4 Test Role 4 Test Director 4
    </tbody>
  </table>

我的循环应该如何显示,以便它创建有效的 HTML 而不是我现在的垃圾?

【问题讨论】:

  • 我在你的代码中看不到任何循环.. 在循环外创建&lt;table&gt; 和标题,然后使用foreach($credits as $credit) { 并创建你的&lt;tr&gt;,所有&lt;td&gt;$credit['credit_id']&lt;/td&gt;s 然后关闭&lt;/tr&gt;。然后最后关闭} foreach 循环,然后关闭&lt;/table&gt;
  • 一个好方法是创建一个函数,将一组值作为参数传递。此函数返回一个字符串,该字符串由数组中的每个值的 、... 和一个最终的 . 组成
  • Ill formatted HTML from a PHP loop 的重复项?尽管此版本中提供的信息质量要好得多,但您似乎忽略了“代码”的循环部分。

标签: php html arrays loops dompdf


【解决方案1】:

当这个question was asked previously 时,我支持my answer。代码很丑,但我相信它会起作用。

但是,这个版本的问题提供了更多关于正在发生的事情的详细信息。我认为更好的解决方案是更新数据数组以更好地反映数据的性质。所以尝试这样的事情:

<?php
$credit_categories = array();
foreach ($records as $record) :
  if (!array_key_exists($record['category_title'], $credit_categories)) :
    $credit_categories[$record['category_title']] = array(
      'category_title' => $record['category_title'],
      'category_position' => $record['category_position'],
      'credits' => array()
    );
  endif;
  $credit_categories[$record['category_title']]['credits'][] = $record;
endforeach;

foreach ($credit_categories as $credit_category) : ?>
  <table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;" class="long-entry">
    <tbody>
      <tr>
        <td width="25%"><strong><?php echo trim($credit_category['category_title']); ?></strong></td>
        <td width="25%"><strong>Title</strong></td>
        <td width="25%"><strong>Role</strong></td>
        <td width="25%"><strong>Director</strong></td>
      </tr>
      <?php foreach ($credit_category['credits'] as $credit) : ?>
      <tr>
        <td width="25%"><?php echo $credit['credit_heading'];?></td>
        <td width="25%"><?php echo $credit['credit_title']; ?></td>
        <td width="25%"><?php echo $credit['credit_role']; ?></td>
        <td width="25%"><?php echo $credit['credit_director']; ?></td>
      </tr>
      <?php endforeach; // end $credit loop ?>
    </tbody>
  </table>
<?php endforeach; // end $credit_categories loop ?>

一些注意事项:

  • 我使用$records 表示您的初始数据数组,因为您没有指定变量。
  • 您可以使用任何东西作为$credit_categories 中的键...只要确保它对于每个类别都是唯一的并且在每个记录中都有表示。我更喜欢使用 ID 而不是文本字段,我只是在使用您提供给我们的内容,我不确定 category_position 是否是唯一的。
  • 如果您更新您的问题以反映“短”信用记录的性质,我可以在必要时进一步完善代码示例,但如果“长”和“短”信用之间没有类别重叠,它可能会按原样工作.

关于数据结构的特别说明:
我已经修改了您现有的数据结构,但您最初应该努力实现这样的结构。大多数 MVC 框架将默认提供规范化的数据结构(如果数据库是规范化的)。如果数据库未规范化,或者这是自定义代码,您的首要任务应该是重新格式化数据数组以更好地反映规范化的数据结构。

另一方面,数据规范化并不总是必要的。此外,根据需要处理的记录数量,规范化数据的代码可能会导致大量开销。因此,请务必提前确定标准化是否会使您的工作更轻松。

【讨论】:

    【解决方案2】:

    第三个数组中的category_title 与前一个相同:“Corporate”,因此代码对第三种情况的作用不同。

    【讨论】:

      【解决方案3】:

      这样的事情可能会做到,尽管我承认这不是最有效的解决方案。

      也许你可以通过使用 for 循环而不是 foreachs 来稍微加快它。

      <?php
      //where $creditArray is the array you're using
      $tables = Array();
      
      foreach ($creditArray as $row) {
           $tables['category_title'][] = $row;
      }
      
      foreach ($tables as $table) {
           echo '
               <table>
                   <tr>
                       <td width="25%">
                           <strong>'.trim($table[0]['category_title']).'</strong>
                       </td>                 
                       <td width="25%"><strong>Title</strong></td>
                       <td width="25%"><strong>Role</strong></td>
                       <td width="25%"><strong>Director</strong></td>
                   </tr>';
      
               foreach ($table as $row) {             
                   echo'<tr>
                          <td width="25%">'.$row['credit_heading'].'</td>
                          <td width="25%">'.$row['credit_title'].'</td>
                          <td width="25%">'.$row['credit_role'].'</td>
                          <td width="25%">'.$row['credit_director'].'></td>
                    </tr>';
               }
      
               echo "</table>";
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 2014-11-17
        • 1970-01-01
        • 1970-01-01
        • 2017-06-16
        • 2021-03-24
        • 1970-01-01
        相关资源
        最近更新 更多