Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8477967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:36:51+00:00 2026-06-10T18:36:51+00:00

I have a loop which shows items in a grid of up to 12

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:36:53+00:00Added an answer on June 10, 2026 at 6:36 pm

    If I understood your question correctly, you would need some code like the following:

    // 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
        }
    }
    

    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.

    // 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 />";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a php loop which displays only one record even if there is
I have a loop in razor which generates a template a number of times.
Well let's say i have a foreach loop which look likes below items =
I have a for loop, inside which I'm calling a method to show the
I have a loop which basically calls this every few seconds (after the timeout):
I have a loop which needs to create an unspecified and indefinite amount of
I have a for-loop which is setting the value of the progress bar on
I have a loop MC which will be duplicate to stage several times according
I have a for-loop which performs the following function: Take a M by 8
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.