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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:42:30+00:00 2026-05-21T06:42:30+00:00

I am stuck working on a problem, and would appreciate some guidance. I am

  • 0

I am stuck working on a problem, and would appreciate some guidance. I am working with an old system that was coded awfully, and didnt do any validating, or sanitzing of user input, so the database I’m working with is a bit missy.

I have issues with one column called “tags”, used for tags for articles. But each row appears like this, and varies between them:

tag tag1 tag2 tag3

OR

tag1, tag2, tag

But I need to combine them and put them into an array that lists them by how many times each one occurs, like this:

  • tag(2)
  • tag1(2)
  • tag2(2)
  • tag3(1)

Because I’m not the most astute with php, Im not sure if I am going about the problem correctly?

Im new to manipulating arrays on this scale which is why I am stuck at this point. You can view the example below of what I am doing below:

  $sql = "SELECT tags, approved FROM articles WHERE approved = '1' ";
  $result = mysql_query($sql);

 while( $rows = mysql_fetch_array($result) ) { 
  $arr = $rows['tags'];
  $arr = str_replace(", ", " ", $arr); // attempting to clean up the data, so that each word appends with a space.
  $arr = str_replace(" ", " ", $arr);

  // Don't know what to do next, or if this is even the right way to do it.

}

Any help is appreciated.

Thanks, Lea

  • 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-21T06:42:31+00:00Added an answer on May 21, 2026 at 6:42 am

    Maybe this will be helpful. Modified your code to have a $tagArr for each query. If you need an overall array of tags it would be a bit different, but could easily be coded using the following.

    while( $rows = mysql_fetch_array($result) ) { 
      $arr = $rows['tags'];
      $arr = str_replace(", ", " ", $arr); // attempting to clean up the data, so that each word appends with a space.
    
      // Don't know what to do next, or if this is even the right way to do it.
    
      $tagArr = array();
      $current_tags = explode(" ", $arr);
      foreach($current_tags as $tag) {
        if(!array_key_exists($tag, $tagArr)) {
          $tagArr[$tag] = 1;
        } else {
          $tagArr[$tag] = $tagArr[$tag]++;
        }
      }
    
      // now $tagArr has the tag name as it's key, and the number of occurrences as it's value
      foreach($tagArr as $tag => $occurrences) {
        echo $tag . '(' . $occurrences . ')<br />';
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am badly stuck on this problem and would appreciate any kinds of helps!
I am stuck with this problem so any help would be appreciated. I have
I am semi-frustrated with this Yii CGridView problem and any help or guidance would
I am working on a problem and got stuck at a wall I have
Working on the N-queens problem. Having some difficult filling up the stack correctly. Was
I am working in MATLAB and I'm stuck on a very simple problem: I've
Im stuck here, any hint would be nice. I have an array of Objects
I have inherited some code that is not working as I think it should:
I had olly 2 Beta which was working fine but it misses some old
I am in need for some help as I am stuck with a problem

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.