Home > Backend Development > PHP Tutorial > Example of how to dynamically read data and clear the rightmost margin using PHP

Example of how to dynamically read data and clear the rightmost margin using PHP

PHPz
Release: 2023-03-07 12:32:02
Original
1844 people have browsed it

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 &#39;<li></li>&#39;;
      }
      ?>
    </ul>
  </p>
</body>
</html>
Copy after login

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>
Copy after login

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 &#39;<li style="&#39;.$margin_r.&#39;">&#39;.$i%$col.&#39;</li>&#39;;
      }
      ?>
    </ul>
  </p>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template