通过向最后一个li添加span类来删除“li”边框的特定php逻辑

时间:2012-09-03 15:52:30

标签: php

我有一个简单的for循环,显示li元素。

for($rn = 1; $rn <= $total; $rn++){
 echo '<li>this is a li element</li>';

}

$ total表示有多少李。

李的左侧漂浮,每行显示6个li,每个li都有一个底部边框。

我希望,使用php通过添加span类来删除最后一行中li的边框。

这有点棘手,因为假设我有15行。

我需要一个PHP代码,它将从15减去12并将无边框类添加到最后3个li。

我的想法:是将$ total除以6并将结果四舍五入。 15:6 = 2.5 向上舍入但记住较小的值 - 将是2。

10乘以2乘以6并从15乘以12,得到3行。

有什么想法吗?

4 个答案:

答案 0 :(得分:2)

模数运算符%通过除以两个数来产生余数。

15 % 6 == 3
$totalRows % $itemsPerRow

答案 1 :(得分:0)

我会这样解决:

for($rn = 1, $end = ( 0 == ( $temp = $total % 6) ? $total - 6 : $total - $temp ); $rn <= $total; $rn++) {
    if ($end < $rn) {
        // no border
    }
}

基本上它检查它是否可以除以6.如果没有休息,则最后六个元素没有边界。如果有休息,则只有最后一行中的元素没有边框。

优点是,它不会调用循环外的任何变量。

循环未经测试,测试计算。

答案 2 :(得分:0)

$remainder = $total % 6; // Get your remainder, number of li on the last row.

for($rn = 1; $rn <= $total; $rn++){
    // If the the total minus the number of li's output is less than or equal to remainder your outputting the last row.
    if($total - $rn <= $remainder) 
    {
        echo '<li class=\'borderless\'>this is a li element</li>';
    }else{
        echo '<li>this is a li element</li>';
    }
}
  • 请注意,这是未经测试的。

答案 3 :(得分:0)

$bottom = $total % 6;
$bottom = $bottom ? $total - $bottom : $total - 6;//calculate values in the last row
for($rn = 1; $rn <= $total; $rn++){
    $class =  $rn > $bottom ? ' class="span"' : '';//if rn is in the last row add span calss
    echo "<li$class>this is a li element</li>";

}