Want to learn how to build a logic?

I am learning Web development currently doing Codeigniter but I am facing difficulty while building a logic for the shopping cart. e.g I want to show 3 products in one row and then 3 and so on I know that will be possible using a count or a loop, but I don’t understand how to implement. It was done after searching from google but I want to be independent.
Any tips to overcome this situation.