vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   Member Archives (https://vborg.vbsupport.ru/forumdisplay.php?f=202)
-   -   Get Total Male/Female Members (https://vborg.vbsupport.ru/showthread.php?t=36046)

grb123 03-12-2002 02:21 PM

Get Total Male/Female Members
 
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

Code:

$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

Code:

$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

or
Code:

SELECT 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?


All times are GMT. The time now is 03:29 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.00998 seconds
  • Memory Usage 1,725KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete