【问题标题】:Make 3 columns with last class in loop在循环中使用最后一个类制作 3 列
【发布时间】:2010-12-13 11:01:24
【问题描述】:

我想制作 3 列并将 last 类添加到第三列。我试过这段代码:

<?php for ($i = 0; $i < 9; $i = $i + 4) { //can't touch this line ?>
<?php for ($j = $i; $j < ($i + 4); $j++) { //can't touch this line ?>

<?php $counter=0; ?>
            <div class="span-5<?php if ($counter % 3 == 0) { echo " last"; } ?>">
Info    
            </div>              


<?php } ?>
<?php } ?>

但这无济于事。 (它将last 类分配给第二列)

【问题讨论】:

  • can't touch this line 是一个方便的评论! :P
  • 代码将最后分配给每一列,因为你的计数器没有增加,所以它总是0。
  • Err.. 计数器在哪里递增?有点难以弄清楚为什么last 目前被应用了!

标签: php html css for-loop blueprint-css


【解决方案1】:

在正确的时间添加$counter++,它会起作用。

<?php for ($i = 0; $i < 9; $i = $i + 4) { //can't touch this line ?>
<?php for ($j = $i; $j < ($i + 4); $j++) { //can't touch this line ?>

<?php $counter=0; ?>
            <div class="span-5<?php $counter++; if ($counter % 3 == 0) { echo " last"; } ?>">
Info    
            </div>              


<?php } ?>
<?php } ?>

【讨论】:

    【解决方案2】:

    试试这个。它可能对你有帮助。

     <?php for ($i = 0; $i < 9; $i = $i + 4) { //can't touch this line ?>
        <?php for ($j = $i; $j < ($i + 4); $j++) { //can't touch this line ?>
    
        <?php if($j == 3){
             $class = 'class="last"';
         }else{
             $class = '';
         } ?>
                    <div <?php $class; ?>>
        Info    
                    </div>    
    
    
    
    
    <?php } ?>
    <?php } ?>
    

    谢谢。

    【讨论】:

    • 嗨,如果你想改变你可以直接改变你的类,也可以为除 3 之外的所有列应用其他类。你的代码也是用这个代码定制的。谢谢。
    【解决方案3】:

    或者只是将您的$counter 设置为$j+1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      • 2021-11-23
      • 2016-09-15
      相关资源
      最近更新 更多