Home >Backend Development >PHP Tutorial >Example of how to dynamically read data and clear the rightmost margin using PHP
The following editor will bring you a method of dynamically reading data and clearing the rightmost margin in PHP. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
Required effect in one row and 3 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 is dynamic reading 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 1 and 2 can achieve the same effect!
The above method of dynamically reading data and clearing the rightmost margin in PHP is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the script. s home.The above is the detailed content of Example of how to dynamically read data and clear the rightmost margin using PHP. For more information, please follow other related articles on the PHP Chinese website!