【问题标题】:foreach loop that requires to stop and start based on whats in a array需要根据数组中的内容停止和启动的 foreach 循环
【发布时间】:2012-01-05 11:31:18
【问题描述】:

我目前在我的代码中将此作为循环,

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

然而这并没有达到我的预期,

我想做的是每次$credit['category_title']改变它的值我想开始一个新表,这可能吗?

======

好的,所以这已经奏效了,我为每个新类别标题获得了一个新表,但是它没有显示该类别标题的所有学分,例如我必须显示商业学分,但它只显示一个,

新代码

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

【问题讨论】:

  • 只有两个不同的值吗?

标签: php arrays loops foreach


【解决方案1】:

存储旧值,如果它改变了做一些事情

foreach($array as $a) {

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


$oldvalue = $a;

}

【讨论】:

  • @ManseUK 同意这是我将这样做的方式,以便将来可以轻松添加另一个类别。
  • 作为旁注,你能看看我的编辑吗?看看我是不是做错了什么?
【解决方案2】:

在 IF 语句中移动表格标签

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

注意这只有在你只有 2 个 credit_type 值时才有效

【讨论】:

  • 只会有长有短
猜你喜欢
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多