PDA

View Full Version : Get Total Male/Female Members


grb123
03-12-2002, 02:21 PM
I've set up a custom profile field for members to enter M for Male and F for Female. I've done similar custom fields for age groups and income, and I now want to display the totals for each group on a stats page.

Basically, I've tried to do a query that fetches info from the custom field Gender (profilefield=19) and prints a total of Male members and a total for Female. So in other words if profilefieldid19 has "M" in it, return the total "M's", and same for "F". Only because I know so little about MySQL I'm not getting very far!

I'm sure it would be very easy for one of you gurus out there!

Admin
03-12-2002, 02:26 PM
$totals = $DB_site->query_first("SELECT SUM(IF(field19='M',1,0)) AS male,SUM(IF(field19='M',1,0)) AS female FROM userfield");
Then use $totals[male] and $totals[female].

You can also do this in two COUNT queries, btw.

grb123
03-12-2002, 02:29 PM
Oh, I wasn't even close!!!

Thanks Firefly!

I take it if I had more than two variables, say for example for the age groups where there are 4 (A, B, C & D where A=Under 18, B=18-30 etc) I would just add extra AS statements as per your answer?

Admin
03-12-2002, 02:31 PM
Yeah, but that could also slow down the query. :)

grb123
03-12-2002, 02:33 PM
Thanks. That shouldn't be a problem because it's only for the site owners, not for general public viewing.

How else would you suggest doing it with more than 2 variables?

Admin
03-12-2002, 02:35 PM
$malecount = $DB_site->query_first("SELECT COUNT(*) AS total FROM userfield WHERE field19='M'");
$femalecount = $DB_site->query_first("SELECT COUNT(*) AS total FROM userfield WHERE field19='F'");
etc.

grb123
03-12-2002, 03:33 PM
Ah, brilliant - thank you very much!

That's kinda what I was trying to do, but you do it so much better!

Freddie Bingham
03-12-2002, 03:34 PM
orSELECT count(*) AS count, field9 AS gender
FROM userfield
WHERE field9 = 'M' or field9 = 'F'
GROUP BY field9

grb123
03-12-2002, 03:47 PM
Brilliant- thanks very much for your help guys! Much appreciated.

grb123
03-12-2002, 04:20 PM
OK, next query...

How can I display the percentage of each variable?

For example, if there are 4 Males and 6 Females (ie 40% Male and 60% Female), how can I automatically take the totals and display the percentages?

Admin
03-12-2002, 04:55 PM
If $male has total of males and $female total of females:
$percentMale = round(($male / ($male+$female) * 100), 1);
$percentFemale = round(($female / ($male+$female) * 100), 1);

grb123
03-12-2002, 06:04 PM
Strange, I get:

Unsupported operand types in <path>.... when I add that query.

Admin
03-12-2002, 06:07 PM
Works fine for me... that's just PHP code, shouldn't go in any query. :)

grb123
03-12-2002, 06:33 PM
Ah, figured it out. I had to change it to the following to work with what I had done:


$percentFemale = round(($female[total] / ($male[total]+$female[total]) * 100), 1);
$percentMale = round(($male[total] / ($male[total]+$female[total]) * 100), 1);

print("<font face=arial, size=2><b>Male: </b>");
print("$male[total] ($percentMale%) &nbsp; ");
print("<b>Female: </b>");
print("$female[total] ($percentFemale%)<p>");



etc

Thanks again for taking the time to help me.

Admin
03-13-2002, 11:43 AM
No probs. :)