在网格中显示项时确定余数-PHP

在网格中显示项时确定余数-PHP,php,modulus,Php,Modulus,我有一个循环,在一个一次最多12个项目的网格中显示项目(3跨4行)。网格中可以有任意数量的项(1到12),但是在一行中只有1到2个项的情况下,我需要向HTML追加一个类。例如: 当我有3,6,9,12项时-无需任何要求 当我有4,7,10个项目(1个余数)-项目4,7和10需要一个类 当我有5,8,11个项目(2个余数)-项目4,5,7,8,10,11需要一个类 如何在PHP中实现这一点。我可以为每个项目提供以下信息: 第页上的产品总数 当前项目 抱歉-编辑者乱说的伪代码: $howmany

我有一个循环,在一个一次最多12个项目的网格中显示项目(3跨4行)。网格中可以有任意数量的项(1到12),但是在一行中只有1到2个项的情况下,我需要向HTML追加一个类。例如:

当我有3,6,9,12项时-无需任何要求 当我有4,7,10个项目(1个余数)-项目4,7和10需要一个类 当我有5,8,11个项目(2个余数)-项目4,5,7,8,10,11需要一个类

如何在PHP中实现这一点。我可以为每个项目提供以下信息:

  • 第页上的产品总数
  • 当前项目
抱歉-编辑者乱说的伪代码:

$howmanyleft = totalproducts - currentproduct
if ($howmanyleft <= 2) {
    if ($currentproduct % 3 == 0) {
        //addclass
    }
}
对不起,我搞错了。这不是我需要的。我道歉。我只需要标记有类的任何剩余项,而不是每一行

如果有4项,则标记第4项 如果有5项,则标记第4项和第5项 如果有10个项目,项目10将被标记
如果有11个项目,项目10和11会被标记

它只是产品的数量乘以列的数量

4 % 3 == 1
5 % 3 == 2
6 % 3 == 0

如果我正确理解了您的问题,您将需要以下代码:

// check how many items will remain in the final row (if the row is not filled with 3 items)
$remainder = $total_items % 3;
for ($i = 0; $i < $total_items; $i++) { 
    if($remainder > 0 && $i >= $total_items - $remainder) {
        // executed for items in the last row, if the number of items in that row is less than 3 (not a complete row)
    } else {
        // executed for items that are in 3 column rows only
    }
}
//检查最后一行将保留多少项(如果该行未填充3项)
$余数=$项目总数%3;
对于($i=0;$i<$total_项;$i++){
如果($restinent>0&&$i>=$total\u项目-$restinent){
//如果最后一行中的项目数小于3(不是完整的行),则对该行中的项目执行
}否则{
//仅对3列行中的项目执行
}
}
下面是一个完整的例子,说明了这样的工作原理。使用以下代码创建一个新的php文件,并查看输出

// add some random data to an array
$data = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven');
$total_items = count($data);

// check how many items will remain in the final row (if the row is not filled with 3 items)
$remainder = $total_items % 3;

// loop through all the items
for ($current_item = 0; $current_item < $total_items; $current_item++) { 
// check to see if the item is one of the items that are in the row that doesn't have 3 items
    if($remainder > 0 && $current_item >= $total_items - $remainder) {
        echo $data[$current_item] . " - item in last row, when row is not complete<br />";
    // code for regular items - the ones that are in the 
    } else {
        echo $data[$current_item] . " - item in filled row<br />";
    }
}
//向数组中添加一些随机数据
$data=数组('1','2','3','4','5','6','7','8','9','10','11');
$total_items=计数($data);
//检查最后一行将保留多少项(如果该行未填充3项)
$余数=$项目总数%3;
//循环浏览所有项目
对于($current_item=0;$current_item<$total_items;$current_item++){
//检查该项目是否为行中没有3个项目的项目之一
如果($resident>0&&$current\u item>=$total\u items-$resident){
echo$data[$current_item]。“-最后一行中的项目,当行未完成时
”; //常规项目的代码-位于 }否则{ echo$data[$current_item]。“-填充行中的项目
”; } }
谢谢你,Lazar,这本来是很理想的,但后来我对PHP做了一些改动,以更简单的方式附加我的类。你愿意粘贴我们正在讨论的部分代码吗?这样我就可以给你一个更充分的答案了?当然。我会更新这个问题。代码是一个JShop网站(www.JShop.co.uk)。让我们一起来看看答案,看看有什么困扰你。。如果我们执行$total_items%3,我们将获得在用3个项目填充所有行后剩余的项目数。所以,如果你总共有10个项目,剩余的是1美元。稍后,在循环中,我们检查$余数是否大于0,然后检查当前项(在本例中为$i)。如果当前项等于或大于($total_items-$restinutes),则只应向项添加类,这意味着符合该条件的项位于最后一行,该行不会有3个项。这有意义吗?这与向n-total<2和n%3!=0
// add some random data to an array
$data = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven');
$total_items = count($data);

// check how many items will remain in the final row (if the row is not filled with 3 items)
$remainder = $total_items % 3;

// loop through all the items
for ($current_item = 0; $current_item < $total_items; $current_item++) { 
// check to see if the item is one of the items that are in the row that doesn't have 3 items
    if($remainder > 0 && $current_item >= $total_items - $remainder) {
        echo $data[$current_item] . " - item in last row, when row is not complete<br />";
    // code for regular items - the ones that are in the 
    } else {
        echo $data[$current_item] . " - item in filled row<br />";
    }
}