0

I have a repeated row which have 5 columns. I want when every time row is looped column data is looped number but continues after every loop. Sample code:

$list = 0;
$list++;
for ($i = 0; $i < 5; $i++ ){
<div class="row">
  <div class="col-2">$list</div>
  <div class="col-2">$list</div>    
  <div class="col-2">$list</div>
  <div class="col-2">$list</div>
  <div class="col-2">$list</div>
</div>
}

Sample result what i want:

<div class="row">
   <div class="col-2">1</div>
   <div class="col-2">2</div>
   ...
   <div class="col-2">5</div>
</div>

<div class="row">
    <div class="col-2">6</div>
    <div class="col-2">7</div>
    ...
   <div class="col-2">10</div>
</div>

Any idea?

3
  • Your sample code only outputs one row; do you have a current attempt that loops over some rows, so that you could show us the output of that? Commented Sep 6, 2018 at 14:01
  • And how many times do you want that to continue looping increasing the value of $list Commented Sep 6, 2018 at 14:01
  • I'm not sure I understand you fully, but maybe it's already sufficient to simply move $list++ into the loop. Just replacing <div>$list</div> with <div>{$list++}</div>` might give you the desired result. Commented Sep 6, 2018 at 14:03

3 Answers 3

1

Use a nested loop to iterate through both your rows and columns while keeping a counter outside of the loop:

$counter = 1;
for ($rowCount = 1; $rowCount < 5; $rowCount++ ) {
    echo '<div class="row">';
    for ($colCount = 1; $colCount < 5; $colCount++ ) {
        echo '<div class="col-2">', $counter, '</div>';
        $counter++;
    }
    echo '</div>';
}

Fiddle: Live Demo

Sign up to request clarification or add additional context in comments.

Comments

0

Simply increment the counter $list as part of the output

<?php
$list = 1;

for ($i = 0; $i < 5; $i++ ){

    echo "<div class='row'>
              <div class='col-2'>$list++</div>
              <div class='col-2'>$list++</div>    
              <div class='col-2'>$list++</div>
              <div class='col-2'>$list++</div>
              <div class='col-2'>$list++</div>
          </div>";
}

Using $list++ it will output the current value and then increment it by one ready for the next line etc etc

Comments

0

You can use two loops. One for how many rows($i) you have second one for iteration (1,2,3,4,5) ($list)

<html>
<body>
<?php
$list = 1;
for ($i = 0; $i < 5; $i++ ){
    echo "<div class='row'>";
    for ($list = 1; $list < 6 ; $list++) {
        echo "<div class='col-2'>$list</div>";
    }
echo "</div>";
}
?>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.