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

The Archive Base Latest Questions

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

Basically I have a foreach loop bringing in data from an array of products

  • 0

Basically I have a foreach loop bringing in data from an array of products in a database which include price etc.. The data is pulled down first by a simple mySQL SELECT query. Which is then stored in an associative array.

I want to count the number of items that get displayed on the page. I know how to count the items in the array but I’m going to be filtering which ones get displayed on the page via urls and would like to just count how many get actually displayed.

I had the idea to just have a variable and + 1 everytime the foreach loop ran but the trouble is I need to echo the number before the loop runs.

Here is my loop:

    <h2>Title</h2><h5><? echo $numberofproducts; ?> products</h5>
    <? 
    $numberofproducts == 0;
    foreach(get_products() as $product){
    $numberofproducts++; 
    // display product info
    ?>

Is there a way to update the h5 tag after the loop has finished running via AJAX? Without the need for another php file with one line in it?

  • 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:08:30+00:00Added an answer on June 10, 2026 at 6:08 pm

    Just go like this:

    <?php $products = get_products(); ?>
    
    <h2>Title</h2><h5><?php echo count($products); ?> products</h5>
    <?php 
    
        foreach($products as $index => $product){
    
            //you may use $index as the progressive loop counter
    
        }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-dimensional array, which basically consists of one sub-array for each year.
I have a TextArea html helper method I'm calling in a foreach loop. Basically,
I have a problem here, Basically. . I have a loop which has a
So I have some data that's coming back from a database query, and the
So i basically have a Stack Panel, And i'm using a foreach loop to
I basically have a program that filters records from one excel file to another
I have a question about PHP foreach loops. I'm using it in Codeigniter. Basically
I have a foreach loop iterating through each object in a vector. When I
I have this foreach statement that is supposed to cycle through an array, but
Ok so i have this php foreach loop <?php foreach ($step_count->rows as $step) {

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.