Я хочу использовать счетчик в качестве пользовательского в цикле while, скажем, у нас есть цикл while <21 Я пытаюсь вывести что-то вроде этого: </p>
1,2,
3,4,
5,6,7,
8,9,
10,11,
12,13,14,
15,16,
17,18,
19,20,21
Я пытался написать эти коды
<?php
$counter = 1;
while ($counter <21) :
if( $counter == 1 || ($counter-1)%4 == 0 ) {
echo $counter."<br>";
}
if( $counter == 2 || ($counter-2)%4 == 0 ) { ?>
<div class="others">
<?php }
if( $counter != 1 && ($counter-1)%4 != 0 ) {
echo $counter.',';
}
if( $counter%4 == 0 ) { ?>
</div>
<?php }
$counter++;
endwhile;
Но вывод такой:
1
2,3,4,
5
6,7,8,
9
10,11,12,
13
14,15,16,
17
18,19,20,
решаемые
@ cannon дает решение, которое работает отлично, но я хочу использовать цикл для вызова сообщений в базе данных, поэтому я решаю проблему ...
<?php
$counter = 1;
$module = 7;
$i = 2;
$arr = [];
while ($counter <22) :
if( $counter == 1 || ($counter-1)% $module == 0 ) {
echo $counter.',';
}
if( $counter == 2 || ($counter-2)% $module == 0 ) {
echo $counter.',<br>';
}
if( $counter == 3 || ($counter-3)% $module == 0 ) {
echo $counter.',';
}
if( $counter == 4 || ($counter-4)% $module == 0 ) {
echo $counter.',<br>';
}
$i = $i + $module;
$arr[] = $i;
if(!in_array($counter,$arr)){
if( $counter == 5 || ($counter-5)% $module == 0 ) {
}
if( $counter != 1 && $counter != 2 && $counter != 3 && ($counter-1)% $module != 0
&& $counter != 4 && ($counter-3)% $module != 0 && ($counter-4)% $module != 0 ) {
echo $counter.',';
}
if( $counter% $module == 0 ) {
echo '<br>';
}
}
$counter++;
endwhile;