我有一个循环遍历数组的foreach循环,并以HTML表格的形式吐出数据。
基本流程如下
<?php foreach($credits as $credit) : ?>
<?php if($credit['credit_type'] == "long") : ?>
<?php if($credit['category_title'] != $oldvalue) : ?>
<?php echo $oldvalue . " changed to ". $credit['category_title']; ?>
<br /><?php echo $count; ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td><?php echo $credit['category_position']; ?></td>
<td><?php echo $credit['category_title']; ?></td>
<td><strong>Title</strong></td>
<td><strong>Role</strong></td>
<td><strong>Director</strong></td>
</tr>
<tr>
<td><?php echo $credit['credit_position']; ?></td>
<td><?php echo $credit['credit_heading']; ?></td>
<td><?php echo $credit['credit_title']; ?></td>
<td><?php echo $credit['credit_role']; ?></td>
<td><?php echo $credit['credit_director']; ?></td>
</tr>
</table>
<?php $oldvalue = $credit['category_title']; ?>
<?php echo $count++; ?>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
我的问题是,在第一个循环中,信用类型是商业的,商业有2个条目,但是循环只打印第一个,而不是第二个,为什么这个?我该如何解决这个问题?
我循环的数组看起来像这样,
Array
(
[0] => Array
(
[credit_id] => 5
[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] => 1
[date_created] => 2012-01-05 11:47:50
[candidates_candidate_id] => 54
[rel_id] =>
[credits_candidate_id] =>
[credits_credit_id] =>
[category_title] =>
[category_position] =>
)
[1] => Array
(
[credit_id] => 2
[credit_heading] => Test Heading 2
[credit_title] => Test Title 2
[credit_role] => Test Role 2
[credit_director] => Test Director 2
[credit_type] => long
[credit_position] => 2
[date_created] => 2012-01-04 07:46:15
[candidates_candidate_id] => 54
[rel_id] =>
[credits_candidate_id] =>
[credits_credit_id] =>
[category_title] =>
[category_position] =>
)
[2] => Array
(
[credit_id] => 4
[credit_heading] => Test Heading 3
[credit_title] => Test Title 3
[credit_role] => Test Role 3
[credit_director] => Test Director 3
[credit_type] => long
[credit_position] => 1
[date_created] => 2012-01-05 10:52:07
[candidates_candidate_id] => 54
[rel_id] => 2
[credits_candidate_id] => 54
[credits_credit_id] => 4
[category_title] => Commercial
[category_position] => 0
)
[3] => 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] => 1
[date_created] => 2012-01-05 11:48:31
[candidates_candidate_id] => 54
[rel_id] => 3
[credits_candidate_id] => 54
[credits_credit_id] => 6
[category_title] => Commercial
[category_position] => 0
)
[4] => 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-04 07:46:15
[candidates_candidate_id] => 54
[rel_id] => 1
[credits_candidate_id] => 54
[credits_credit_id] => 1
[category_title] => Television
[category_position] => 1
)
)
,最终输出看起来像这样,
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>0</td>
<td>Commercial</td>
<td><strong>Title</strong></td>
<td><strong>Role</strong></td>
<td><strong>Director</strong></td>
</tr>
<tr>
<td>1</td>
<td>Test Heading 3</td>
<td>Test Title 3</td>
<td>Test Role 3</td>
<td>Test Director 3</td>
</tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>1</td>
<td>Television</td>
<td><strong>Title</strong></td>
<td><strong>Role</strong></td>
<td><strong>Director</strong></td>
</tr>
<tr>
<td>1</td>
<td>Test Heading 1</td>
<td>Test Title 1</td>
<td>Test Role 1</td>
<td>Test Director 1</td>
</tr>
</table>
答案 0 :(得分:2)
如果category_title
不同,您只能输出表格:
<?php if($credit['category_title'] != $oldvalue) : ?>
使用以下工作:
<?php foreach($credits as $credit) : ?>
<?php if($credit['credit_type'] == "short") : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td><?php echo $credit['category_position']; ?></td>
<td><?php echo $credit['category_title']; ?></td>
</tr>
<tr>
<td><?php echo $credit['credit_heading']; ?></td>
<td><a href="">Edit</a></td>
</tr>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "long") : ?>
<?php if($credit['category_title'] != $oldvalue) : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<?php endif; ?>
<tr>
<td><?php echo $credit['category_position']; ?></td>
<td><?php echo $credit['category_title']; ?></td>
<td><strong>Title</strong></td>
<td><strong>Role</strong></td>
<td><strong>Director</strong></td>
</tr>
<tr>
<td><?php echo $credit['credit_position']; ?></td>
<td><?php echo $credit['credit_heading']; ?></td>
<td><?php echo $credit['credit_title']; ?></td>
<td><?php echo $credit['credit_role']; ?></td>
<td><?php echo $credit['credit_director']; ?></td>
</tr>
<?php $oldvalue = $credit['category_title']; ?>
<?php if($credit['category_title'] != $oldvalue) : ?>
</table>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
</table>
答案 1 :(得分:0)
错误是:
<?php if($credit['category_title'] != $oldvalue) : ?>
// you goes farther only if category changed, so the second etry isn't posting.
插入后:
<br /><?php echo $count; ?>
这样:
<?php endif; ?>
最后替换一个endif。