douwei2713 2017-01-27 12:54
浏览 51
已采纳

PHP计数器不起作用

I created a short code, where the counter is used to count up the elements, but the counter counts only till "1", why?

<?php $counter = "0";?>
<?php foreach($this->group('inhalt') as $i => $fields): ?>
    <?php $counter += 1 ;?>
    <div class="ce_countup autogrid-id_<?php echo $this->id; ?>_<?php echo $counter;?> autogrid-type_cte <?php if($counter ="1"){ echo "autogrid-first";} ?> <?php if($counter =$gridKlasse){ echo "autogrid-last";} ?> <?php echo $gridKlasse;?> autogrid_mode_auto autogrid block">
        <h3 id="ce_id_<?php echo $this->id; ?>_<?php echo $counter;?>"><?php echo $this->field('zahl#'.$i)->value(); ?></h3>
        <?php echo $this->field('bezeichnung#'.$i)->value(); ?>
    </div>   
<?php endforeach; ?>
  • 写回答

1条回答 默认 最新

  • dtwye28880 2017-01-27 13:00
    关注

    Your problem is here

    <?php if($counter ="1"){ echo "autogrid-first";} ?>
    

    and here

    <?php if($counter = $gridKlasse){ echo "autogrid-last";} ?>
    

    You're always re-initializing the $counter variable with 1 and then doing a boolean evaluation of the whole expression. You should use either == or === to fix the problem. Also, $gridKlasse should be a number.

    Try this:

    <?php $counter = 0;?>
        <?php foreach($this->group('inhalt') as $i => $fields): ?>
        <?php $counter += 1 ;?>
        <div class="ce_countup autogrid-id_<?php echo $this->id; ?>_<?php echo $counter;?> autogrid-type_cte <?php if($counter === 1){ echo "autogrid-first";} ?> <?php if($counter === $gridKlasse){ echo "autogrid-last";} ?> <?php echo $gridKlasse;?> autogrid_mode_auto autogrid block">
            <h3 id="ce_id_<?php echo $this->id; ?>_<?php echo $counter;?>"><?php echo $this->field('zahl#'.$i)->value(); ?></h3>
            <?php echo $this->field('bezeichnung#'.$i)->value(); ?>
        </div>   
    <?php endforeach; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型