douyanjing0822 2018-10-05 07:44
浏览 20
已采纳

如何在foreach循环上打印出预定的数字?

<?php 
$stmt = $db->query("SELECT * FROM table LIMIT 5"); 
foreach ($AnB as $aB): ?>
  <a href="#">
    <div>
      <h6>Number here</h6>
      <h5><?php echo $aB['title'] ?></h5>
    </div>
  </a>
<?php endforeach; ?>

As you can see, i used a LIMIT 5, i want to put a number on each loop, starting on number 1 till 5. How can i do it?

This is how it should be:

enter image description here

  • 写回答

1条回答 默认 最新

  • doupuxuan5784 2018-10-05 07:45
    关注
       <?php 
        $count = 1;
        $stmt = $db->query("SELECT * FROM table LIMIT 5"); 
        foreach ($AnB as $aB): ?>
          <a href="#">
            <div>
              <h6><?php echo $count; ?></h6>
              <h5><?php echo $aB['title'] ?></h5>
            </div>
          </a>
        <?php $count++; endforeach; ?>
    

    I used a variable to maintain the count. It will add 1 to the $count variable each time it loops through the foreach

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services