php - Display the count and percentage from result -


$query = "select title accesslog order title asc";  $result = db_query($query);         while ( $dataresult = $result->fetchassoc() ) {             echo '<a href='. $dataresult["path"].' >'.$dataresult["title"].'</a><br>';         } 

it gives result

title 1
title 1
title 1
title 1
title 2
title 2
title 2
title 3
title 3
title 4
title 5
title 5
title 5
title 5
title 6
title 6
title 6
title 7
title 8
title 8
title 9
title 9
title 9
title 10
title 10
title 10

i need rewrite as

total 26

title 1 - 4 - 0.2(4/20)
title 2 - 3 - 0.15(3/20)
title 3 - 2 - 0.1(2/20)
title 4 - 1 - 0.05(1/20)
title 5 - 4 - 0.2(4/20)
title 6 - 3 - 0.15(3/20)
title 7 - 1 - 0.1(1/20)
title 8 - 2 - 0.2(4/20)
title 9 - 3 - 0.15(3/20)
title 10 - 3 - 0.15(3/20)

can done sql or php.

in databases, can do:

select title, count(*), count(*) * 1.0 / count(*) on () table t group title 

(you can "3 / 20" if want putting columns separately.)

many php users use mysql, not support syntax. instead:

select t.title, count(*), count(*) * 1.0 / total.total table t cross join      (select count(*) total table t) total group t.title; 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -