Home  >  Article  >  Backend Development  >  PHP method to dynamically read data and clear the rightmost margin

PHP method to dynamically read data and clear the rightmost margin

墨辰丷
墨辰丷Original
2018-05-24 09:35:141166browse

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


Related recommendations:

Framework Thinkphp5 Simple implementation of behavioral hooks Hook

The usage scenarios of the four php functions shell_exec, exec, passthru, and system respectively

php uses __call overloading

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!

Statement:
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