So firstly you can pre-increment an integer variable by prepending ++
to it, e.g.
++$i
. That will work for you, e.g.
'<div class= "col-lg-3 order-lg-'.++$i.' col-md-6 p-0">'
However, you also have another problem which is preventing you from seeing the results as you are expecting. Every time you write $html =
- which is twice within your loop, you overwrite the contents of $html
. The previous contents of the variable are destroyed. Therefore, at whatever time you come to echo $html
to the output, you'll only ever see the last version.
You need to concatenate all the HTML together into a single string without overwriting it.
Put
$html = "";
just before the loop starts, and then change
$html ='<div class= "col-lg-3 order-lg-'.$i.' col-md-6 p-0">';
to
$html .='<div class= "col-lg-3 order-lg-'.++$i.' col-md-6 p-0">';
(note the .=
there, and also the ++$i
which you forgot)
and lower down, change
$html ='<div class= "col-lg-3 order-lg-"'.++$i.'"col-md-6 p-0">';
to
$html .='<div class= "col-lg-3 order-lg-"'.++$i.'"col-md-6 p-0">';
(again just changing the =
to .=
.)
Demo: http://sandbox.onlinephpfunctions.com/code/7e1ac7b039867eedd22d90dfcdc03e8990419a8f
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…