I have a loop which shows items in a grid of up to 12 items at a time (3 across by 4 rows down). There can be any number of items in the grid (1 to 12), but on instances where I have only 1 or 2 items on a row, I need to append a class to the HTML. For instance:
When I have 3,6,9,12 items – nothing required
When I have 4,7,10 items (1 remainder) – Items 4, 7 and 10 need a class applying
When I have 5,8,11 items (2 remainder) – Items 4,5, 7,8, 10,11 need a class applying
How can I do this in PHP. I have the following available to me for each item:
- Total number of products on page
- Current Item
Apologies – pseudo code as the editor garbles it:
$howmanyleft = totalproducts - currentproduct
if ($howmanyleft <= 2) {
if ($currentproduct % 3 == 0) {
//addclass
}
}
Then in my CSS
article.product-single {
width: 33.3333%;
border-bottom: 1px solid rgb(195,195,195);
border-right: 1px solid rgb(195,195,195);
}
article.product-single:nth-child(3n) {
border-right: none;
}
article.lastrow, article.product-single:last-child {
border-bottom:none;
}
Sorry, I’ve got this wrong. This isn’t what I need. My apologies. I just need any remaining items flagged with a class, not every row.
If there’s 4 items, item 4 gets flagged
If there’s 5 items, items 4 and 5 get flagged
If there’s 10 items, item 10 gets flagged
If there’s 11 items, items 10 and 11 get flagged
If I understood your question correctly, you would need some code like the following:
Here is a complete example of how something like this would work. Create a new php file with the following code and see the output.