foreach循环忽略数组内容
问题描述:
我有一个foreach循环遍历数组,并以HTML表格的形式吐出数据。foreach循环忽略数组内容
基本流程如下
<?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>
答
你唯一的输出表,如果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>
答
的错误是:
<?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。
$是一个mysql结果? – 2012-01-05 14:41:44
我认为它与'if($ credit ['category_title']!= $ oldvalue)'有关。您正在使用相同的'category_title'跳过选项。 – 2012-01-05 14:42:52