简体   繁体   中英

php set top property different after every 3 div dynamically

I want to set the css TOP property different after every 3 div's

for eg:

<div style="top:100px;">
</div>
<div style="top:100px;">
</div>
<div style="top:100px;">
</div>
**Now after this the next 3 div's shall have**
<div style="top:150px;">
</div>
<div style="top:150px;">
</div>
<div style="top:150px;">
</div>
**Now after this the next 3 div's shall have**
<div style="top:200px;">
</div>
<div style="top:200px;">
</div>
<div style="top:200px;">
</div>
**and so on**

I want to do this dynamically using php. Using the for loop i can check if

$ij=0; if($ij%3==0) {
    echo "";
    }
    else {
    }

But not sure how to get desired result.

Any help is really appreciated.

Try this:

$top = 50;
$n = 30; // no. of divs
for($i = 0; $i < $n; $i++) {
    if($i % 3==0) $top+=50;
    echo "<div style=\"top:".$top."px;\">\n</div>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM