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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:20:20+00:00 2026-05-28T16:20:20+00:00

I have categories currently in my HTML code as follows: <ul> <li>Category 1: ($Count)</li>

  • 0

I have categories currently in my HTML code as follows:

<ul>
    <li>Category 1: ($Count)</li>
    <li>Category 2: ($Count)</li>
    <li>Category 3: ($Count)</li>
</ul>

I want list the count of number of elements each category contain. i could get the count a of category using following SQL syntax:

SELECT COUNT(element) FROM category WHERE cid = 1

However, I don’t know how to bind this up to each category in the HTML code.

I can only think of one other way of implementing this: make the category name dynamic by storing it also in the database, then select all the categories with count.

  • 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-28T16:20:21+00:00Added an answer on May 28, 2026 at 4:20 pm

    I think you it will be better if you could store category name in database. If you store Category name in database like

    categories
    +----+----------------------+
     cid | cat_name                 |
    +----+----------------------+
    | 1  | Category 1
    | 2  | Category 2
    | 3  | Category 3
    ...
    

    Then you could query database like:

    $query = "SELECT count(*) as count,`cat_name` FROM categories GROUP BY cid";
    
    
    <ul>
       <?php foreach($categories as $category) { // Or however you get it ?>
          <li><?php echo htmlentities($category['cat_name']); ?>: (<?php echo $category['count']; ?>)</li>
      <?php } ?>
     </ul>
    

    If you don’t store the Category Name in db, another possibility is that if your cid 1 is same as category 1 and so on then you can do.

    $query = "SELECT count(*) as count,`cid` FROM category GROUP BY cid ORDER BY cid ASC"
    

    Provided the $result is in array in order then you can do

    <ul>
       <li>Category 1: ($result[0][count])</li>
       <li>Category 2: ($result[1][count])</li>
       <li>Category 3: ($result[2][count])</li>
    </ul>
    

    But i recommend you to use first one.

    Hope this helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Categories table in which each category has a ParentId that can
I have 4 categories within the 'Default Category' category. Currently by default Magento is
I have a set of divs that represent story categories. In each category, there's
I currently have this code: shop_category.shop_sub_categories.each do |sub_category| products << sub_category.products end products =
I currently have associations like this: Category belongs_to User User has_many Categories A User
I have list of categories , some have sub-category and some have sub-sub-category which
I currently have a site with a hard-coded list of categories & subcategories, where
I have about 200 categories that are nested. I am currently reading the documention
I have main categories and sub categories. I want to be able to sort
I have a 'Categories' table. Each row has a 'CategoryId' (required), and a 'ParentCategoryId'

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.