Udders
Udders

Reputation: 6976

foreach loop that requires to stop and start based on whats in a array

I currently have this as a loop in my code,

    <table width="100%" cellpadding="0" cellspacing="0" border="0">
    <?php foreach($credits as $credit) : ?>
        <?php if($credit['credit_type'] == "short") : ?>
            <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>
        <?php endif; ?>
        <?php if($credit['credit_type'] == "long") : ?>
            <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 endif; ?>
    <?php endforeach; ?>
</table>

However this is not doing what I had hoped,

What I wanting to do is that everytime, $credit['category_title'] changes it value I want to start a new table, is this possible?

======

OK, so this has worked and I am getting a new table for every new category title, however it is not showing all the credits with that category title, for example I have to commercial credits but it is only showing one,

new code

<?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") : ?>
       <table width="100%" cellpadding="0" cellspacing="0" border="0">
            <?php echo $oldvalue . " changed to ". $credit['category_title']; ?>
            <?php if($credit['category_title'] != $oldvalue) : ?>
                <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 endif; ?>
       </table>
    <?php endif; ?>
<?php endforeach; ?>

Upvotes: 0

Views: 307

Answers (2)

Lee
Lee

Reputation: 10603

store the old value, and if it changes do something

foreach($array as $a) {

if($a != $oldvalue)
 echo "end table, start table";


$oldvalue = $a;

}

Upvotes: 4

Manse
Manse

Reputation: 38147

Move the table tags inside the IF statements

<?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") : ?>
       <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 endif; ?>
<?php endforeach; ?>

Note this is only going to work if you only ever have 2 credit_type values

Upvotes: 3

Related Questions