цикл foreach, игнорирующий содержимое массива - PullRequest
0 голосов
/ 05 января 2012

У меня есть цикл 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>

Ответы [ 2 ]

2 голосов
/ 05 января 2012

Вы выводите таблицу только, если 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 голосов
/ 05 января 2012

Ошибка:

<?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; ?>

конец заменить один конец в конце.

...