我有一個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>
$是一個mysql結果? – 2012-01-05 14:41:44
我認爲它與'if($ credit ['category_title']!= $ oldvalue)'有關。您正在使用相同的'category_title'跳過選項。 – 2012-01-05 14:42:52