Home >Backend Development >PHP Tutorial >PHP method to dynamically read data and clear the rightmost margin
This article mainly introduces the method of dynamically reading data and clearing the rightmost margin in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
Requirement effect in one row and three columns:
Scenario simulation: A colleague gave me this piece of static code As follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <style> li,ul{padding: 0;margin:0;list-style: none;} .box{ width:1000px;background: #ddd;height:500px; } .box li{margin:0 50px 20px 0;background:red;height:30px;width:300px;float: left;} </style> <body> <p class="box"> <ul> <?php for($i=0;$i<9;$i++){ echo '<li></li>'; } ?> </ul> </p> </body> </html>
But the dynamic reading is unified? What should I do if the width is not enough? The wrong line wrapping effect is not what we want!
Solution 1: Style widening and hiding
<style> li,ul{padding: 0;margin:0;list-style: none;} .box{ width:1000px;background: #ddd;height:500px;overflow: hidden; } .box ul{width: 1200px;} .box li{margin:0 50px 20px 0;background:red;height:30px;width:300px;float: left;} </style>
Preview is normal:
Solution 2: PHP judgment, clear the rightmost column Margin
<p class="box"> <ul> <?php //列数 $col=3; for($i=0;$i<9;$i++){ $margin_r = (($i%$col)==($col-1))?"margin-right:0;":"";//清除每行最右侧宝贝右边距 echo '<li style="'.$margin_r.'">'.$i%$col.'</li>'; } ?> </ul> </p>
##Option one and option two are both possible Achieve the same effect!
The above is the entire content of this article, I hope it will be helpful to everyone's study. Framework Thinkphp5 Simple implementation of behavioral hooks Hook
The usage scenarios of the four php functions shell_exec, exec, passthru, and system respectively
The above is the detailed content of PHP method to dynamically read data and clear the rightmost margin. For more information, please follow other related articles on the PHP Chinese website!