Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
Need help fixing this SQL query ( w/ PHP)
Post Flair (click to view more posts with a particular flair)
Post Body

The query (I apologize, PHP doesn't display well in Codepen):

https://codepen.io/QuietWind01/pen/poaeVbE

Basically I have 4 columns (tag1, tag2, tag3 & tag4) that I want to be able to count (then display) the number of times that value shows up in the table. (in the WHERE clause, 'cat' will actually be equal to the variable of $tag(1-4), pulled from a previous query; $tag1 for example being equal to $row['tag1'] )

It's hard to explain, but basically I have a while statement in my main query when displaying the tags to the page. I basically want to not only display ALL of the values in ALL the rows from columns tag1-tag4, but display a count next to each one (eg.: cat: 5 tags), while ALSO only displaying EACH unique/distinct value ONCE. (Normally an SQL query like this would display 'cat' as many times as it shows in each of those 4 table columns).

Basically: Let's say I have col1-col4. Amount of rows wouldn't matter. The unique values in those rows include: cat, dog, rabbit, bird, snake. I would like it to display on the page like this: cat: 3 tags, dog: 8 tags, rabbit: 2 tags, bird: 13 tags, snake: 1 tag

I've tried numerous different things both in my code, and in my SQL query... no matter what or how close I get, I can't figure out a good method for it, and scouring the internet I at BEST get an answer showing how to count the distinct number of values in a single row--which is easy enough--but that doesn't include doing it for multiple rows while also filtering out repeated values, for example, so it won't spit out "cat: 3tags" multiple times.

I know I'm better than this but for the life of me I just can't seem to get it to work... I was hoping I'd be able to do this purely in SQL query, if not, I can easily provide you with the code I'm working with. Thank you!

Author
Account Strength
90%
Account Age
4 years
Verified Email
Yes
Verified Flair
No
Total Karma
898
Link Karma
192
Comment Karma
706
Profile updated: 1 day ago
Posts updated: 9 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago