I have a foreach loop that loops through an array, and spits out data in the form of a HTML table.

The basic flow is as follows

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

My problem is that in the first loop the credit type is commercial, and commercial has 2 entries however the loop only ever prints the first one, and not the second one, why is this? How can I resolve this?

The array I looping through looks like this,

    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
        )

)

and the final output looks likes this,

<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>
share|improve this question

72% accept rate
Is $credits a mysql result? – Hans Wassink Jan 5 at 14:41
I think it has something to do with if($credit['category_title'] != $oldvalue). You're skipping options with the same category_title. – Rocket Hazmat Jan 5 at 14:42
feedback

2 Answers

up vote 2 down vote accepted

Your only outputting the table if the category_title is different :

<?php if($credit['category_title'] != $oldvalue) : ?>

Using the following worked :

    <?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>
share|improve this answer
I am unsure as to how to implement your answer, I though I had done it correctly. – sico87 Jan 5 at 14:56
You didnt implement my answer - you implemented a different one - one that obviously doesnt work ... – ManseUK Jan 5 at 14:57
You answer was extremely hard to follow, it bares no relevance to my example code – sico87 Jan 5 at 15:22
@sico87 really ? stackoverflow.com/questions/8741760/… I gave you the entire source – ManseUK Jan 5 at 15:36
really I have used your code, but it adds a new table each time, I only want a new table when a new category title is found, other I just need to append a new row. – sico87 Jan 5 at 15:41
show 2 more comments
feedback

The error is:

<?php if($credit['category_title'] != $oldvalue) : ?>
// you goes farther only if category changed, so the second etry isn't posting.

Insert after:

<br /><?php echo $count; ?>

this:

<?php endif; ?>

end replace one endif in the end.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.