2012-01-05 102 views
0

我有一個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> 
+0

$是一個mysql結果? – 2012-01-05 14:41:44

+0

我認爲它與'if($ credit ['category_title']!= $ oldvalue)'有關。您正在使用相同的'category_title'跳過選項。 – 2012-01-05 14:42:52

回答

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> 
+0

我不確定如何實現你的答案,但我已經做對了。 – Udders 2012-01-05 14:56:30

+0

你沒有實現我的答案 - 你實現了一個不同的 - 一個顯然不工作... – ManseUK 2012-01-05 14:57:53

+0

你的回答非常難以遵循,它真的與我的示例代碼 – Udders 2012-01-05 15:22:11

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。