foreach循环,需要根据数组中的内容停止和启动 [英] foreach loop that requires to stop and start based on whats in a array

查看:128
本文介绍了foreach循环,需要根据数组中的内容停止和启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在在我的代码中有一个循环,

 < table width =100%cellpadding = 0cellspacing =0border =0> 
<?php foreach($ credit 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 =>编辑< / 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>标题< / strong>< / td>
< td>< strong>角色< / 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>

然而这并不是我所希望的,
$ b $我想要做的是每次 $ credit ['category_title'] 改变它的值我想开始一个新表,这是可能的吗?

新代码

 <?php foreach($ credit as $ credit):?> 
<?php if($ credit ['credit_type'] ==short):?>
< table width =100%cellpadding =0cellspacing =0border =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 =>编辑< / a>< / td>
< / tr>
< / table>
<?php endif; ?>
<?php if($ credit ['credit_type'] ==long):?>
< table width =100%cellpadding =0cellspacing =0border =0>
<?php echo $ oldvalue。 变成 。 $信贷[ 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>标题< / strong>< / td>
< td>< strong>角色< / 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; ?>


解决方案

>

 <?php foreach($ credit as $ credit):?> 
<?php if($ credit ['credit_type'] ==short):?>
< table width =100%cellpadding =0cellspacing =0border =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 =>编辑< / a>< / td>
< / tr>
< / table>
<?php endif; ?>
<?php if($ credit ['credit_type'] ==long):?>
< table width =100%cellpadding =0cellspacing =0border =0>
< tr>
< td><?php echo $ credit ['category_position']; ?>< / TD>
< td><?php echo $ credit ['category_title']; ?>< / TD>
< td>< strong>标题< / strong>< / td>
< td>< strong>角色< / 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; ?>

注意只有当您只有2 credit_type


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

解决方案

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

这篇关于foreach循环,需要根据数组中的内容停止和启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆