Natalie
Natalie

Reputation: 77

How to print out a predetermined number on foreach loop?

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

Upvotes: 0

Views: 150

Answers (1)

E3Im
E3Im

Reputation: 330

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

Upvotes: 6

Related Questions