【发布时间】: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 而不是我现在的垃圾?
【问题讨论】:
-
我在你的代码中看不到任何循环.. 在循环外创建
<table>和标题,然后使用foreach($credits as $credit) {并创建你的<tr>,所有<td>$credit['credit_id']</td>s 然后关闭</tr>。然后最后关闭}foreach 循环,然后关闭</table> -
一个好方法是创建一个函数,将一组值作为参数传递。此函数返回一个字符串,该字符串由数组中的每个值的
、 . 组成... 和一个最终的 -
Ill formatted HTML from a PHP loop 的重复项?尽管此版本中提供的信息质量要好得多,但您似乎忽略了“代码”的循环部分。
标签: php html arrays loops dompdf