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 6544703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:29:50+00:00 2026-05-25T11:29:50+00:00

I am using PHP via Codeigniter for this scenario. It’s a basic category listing

  • 0

I am using PHP via Codeigniter for this scenario.

It’s a basic category listing that has items assigned with to those categories. What I’m having problem with is showing the item count beside the category names on the category listing page.

How I was doing it was a very ineffective way i think, and I would really like to know what would be the common practice for this sort of thing.

So I was getting the category list in my controller, and then in my view, on the loop thru the categories, I call a model method to grab the count of the respective cat_id on every iteration. (horror!)

Should I be:

A) Getting the categories AND it’s item count from 1 SQL statement

OR

B) Process the category list in the controller and get the categories’ corresponding item counts in the controller

If so, how to ?

  • 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-05-25T11:29:51+00:00Added an answer on May 25, 2026 at 11:29 am

    I would prefer to have one query with everything I need, if your DB design allow to do so.

    Assuming you have a Categories table and a Products table you should be able to do something like this:

    Select C.Category, Count(P.Id) as ProductsCount 
    From Categories C left join Products P on C.Id = P.Category_Id
    Group by C.Category    
    Order by C.Category
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have any experience using PHP to send an sms via skype I
I'm getting the following using the PHP client on my server (connecting via FBML).
Using PHP, what's the fastest way to convert a string like this: 123 to
I've just started playing with Google App Engine using php via Quercus. I'm not
I'm passing a large dataset into a MySQL table via PHP using insert commands
I'm writing a script that can FTP files and folders using PHP functionality. I'm
I need to escape entire javascript code block using escape() compatible function via PHP,
Using PHP, I am building an application that is MySQL database resource heavy, but
I am new to codeigniter and i am installing this shopping cart that is
I'm working on a Codeigniter application that can be accessed via Smartphones. The thing

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.