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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:09:35+00:00 2026-05-18T10:09:35+00:00

Not sure this is an architecture question or not, please fix if i am

  • 0

Not sure this is an architecture question or not, please fix if i am wrong,
ok i have a arrays ( by using a foreach )

foreach ($db['products'] as $product) 
{
    echo '<li><a href="">' . $product['tag'] . '</a></li>';
}

 // displays like
 tag1
 tag4
 tag2
 tag2
 tag1
 tag3
 tag1
 tag1
 tag3
 tag2
 tag3
 tag2

do anyone have an idea how to show

//count tags & no duplicate tags ?
tag1(4)
tag2(4)
tag3(3)

like stackoverflow have ?

edit one ( PDO )

$database->fetchall("SELECT pid, uid, name, information, image_tumb, tag, price 
FROM products ORDER BY pid DESC LIMIT 100");

edit two when i do this

$db['products'] = $database->fetchall("SELECT pid, uid, name, information, image_tumb, tag, price 
FROM products GROUP BY tag ORDER BY pid DESC LIMIT 100");

it does work, but some products are not displaying.

  • all product is displayed
  • but the tags are just like above.

thanks for your time btw, its save my learning time alot.

edit* it work. thanks all

$db['products'] = $database->fetchall("SELECT pid, uid, name, information, image_tumb, price 
FROM products ORDER BY pid DESC LIMIT 100");

$db['tags'] = $database->fetchall("SELECT tag, COUNT(tag) AS counter
FROM products GROUP BY tag ORDER BY tag DESC LIMIT 100");


        foreach ($db['tags'] as $tag) {
            echo '<li><a href="">' . $tag['tag'] . '(' . $tag['counter'] . ')</a></li>';
        }

buy doing this i still get all of my products with a list of tags no duplicate + counter. thanks again!.

  • 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-18T10:09:36+00:00Added an answer on May 18, 2026 at 10:09 am

    You can either use an array to remember the items that already have been displayed:

    $items = array();
    foreach ($db['products'] as $product) {
        if (!isset($items[$product['tag']]) {
            $items = 1;
        } else {
            $items++;
        }
    }
    foreach ($items as $tag => $count) {
        echo '<li><a href="">' . $product['tag'] . ' (' . $count . ')</a></li>';
    }
    

    Or you sort the array in advance and just compare the current item with the previous one:

    array_multisort($db['products'], array_map(function($item) { return $item['tag']; }, $db['products']));
    $previous = null;
    foreach ($db['products'] as $product) {
        if ($product['tag'] !== $previous['tag']) {
            if ($previous !== null) {
                echo '<li><a href="">' . $product['tag'] . ' (' . $counter . ')</a></li>';
            }
            $counter = 0;
        } else {
            $counter++;
        }
    }
    

    But you can probably do the same within your database query and just grab the results like:

    SELECT tag, COUNT(tag) AS counter
    FROM table
    GROUP BY tag
    ORDER BY tag
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure this is a programming question, but we use LaTeX for all our
Not sure if this is intended behavior or a bug or a wrong function
I´m not sure if this is really a programming question, but it's related to
I am pretty sure this is possible just not sure what is the term
Not sure if this is possible or if I'm expressing correctly what I'm looking
I'm not sure if this is something I should do in T-SQL or not,
I'm not sure if this is a strange thing to do or not, or
I'm not sure if this even exists or not, so I figured I would
I'm not sure what this practice is actually called, so perhaps someone can edit
I'm not sure if this a settings problem or an HTML problem, but on

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.