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