Jump to content

Create graph with PHP / MySQL by counting duplicate entries


David Nelson

Recommended Posts

Hey guys.

 

I'd like to create bar graphs based off of duplicate data retrieved from a MySQL db. I have a general *idea* of how this can be done, but the code itself isn't standing out to me at the moment.

 

Here's an easy example of something similar to what I'll be doing with this:

 

Let's say we have a form on a site where users can type a day of the week into an input box. When a user types "Thursday" - for example - and presses Go, PHP creates a row in the mysql table with the timestamp and what the user typed which is, in this case, "Thursday".

 

Now lets say a few users enter a day of the week into the DB. Our mysql table might have values similar to this:

 

Monday

Thursday

Wednesday

Friday

Thursday

Thursday

Thursday

Tuesday

Tuesday

 

So - 1 monday, 4 thursdays, 1 friday and 2 tuesdays.

 

Now, I would like to SELECT from the DB and draw out a simple CSS bar graph stating the most popular (duplicate) values from the table. It might look something like

 

Thursday |||||||||||||||

Tuesday ||||||||

Monday ||

Friday ||

 

Because Thursday had the most occurances, Tuesday the second, and Monday/Friday both had 1 occurance.

 

This example is the easiest way I could come up with to demonstrate what I'm looking for. Unlike the simplified example I gave, the possible values will not be days of the week, and therefore will not be predictable. Obviously if I was just making a poll for people to choose a random day of the week I wouldn't go about it this way.

 

I *think* the general way to go about this will involve making some sort of loop and getting variables setup to count dupes and eventually multiply the values of those counters to get percentage values to pass into a CSS div or something to represent the bar graphs.

 

This sounds kind of tricky, but I think it is overall pretty simple, so let me know if you need any clarification.

 

Thanks!

 

David

Well If You know how to get the info out of the database, then like You said simply add as many dots/bars to the html. Then ofc use css to make them to look as you like.

 

For the SQL part use the count() function to count the number of hits in the db, so you wont have to loop through a whole bunch of results, but just You will get one row of data.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.