Kreation
Kreation

Reputation: 327

Displaying mysql query in specific tags for wordpress

This is the query I'm currently using. It's purpose is to take a percentage of all the values in a column, and output the highest percentage value.

<?php
$wpdb->query( 
"
SELECT COUNT(field1) as totals FROM test GROUP BY field1 ORDER BY totals DESC",
);
?>

Here is the structure of the data I'm working with:

field1|
15
15
15
17
13
12
15
15
17
17
18

This is my desired output:

field1|
15 - 45.4%
17 - 27.2%
13 - 9.0%
12 - 9.0%
18 - 9.0%

And this is what I need the HTML output to look like:

<div class="field1">15</div>
<div class="field2"></div>
<div class="field3"></div>... and so on

My goal is to be able to take the output from the query, and display it in a specific div or td tag.

However, I would like to have this set up so I don't need separate php files for each and every entry/field in the database that I'm trying to display.

I also need this to be flexible enough to allow me to add additional inputs should I need to update the page and add more information.

Upvotes: 1

Views: 131

Answers (1)

tuffkid
tuffkid

Reputation: 1323

To get percentages of your data, you need to change your sql query

`select round((count(*)*100)/(select count(*) from test),1) as percent from test group by field1 order by percent desc`

Here is fiddle for your data

Upvotes: 1

Related Questions