Author Topic: Gender Table Help  (Read 1875 times)

Offline Pug307

  • Level 21
  • *
  • Posts: 244
  • Reputation: +6/-1
    • View Profile
Gender Table Help
« on: October 15, 2006, 09:18:09 PM »
I have a table in my game called gender which is a sub table to users, In the signup form you can select Male or Female. This adds the data to the table gender. What I am wanting to do is setup a part in my index page which tells you how many males and how many females are registered. How would I use a query for this?

EXAMPLE - Males 89, Females 42

This is just so the members know how many males and females are registered in the game.

Thank you in advance



Offline dvd871

  • Level 21
  • *
  • Posts: 238
  • Reputation: +7/-0
    • View Profile
    • Dominion Siege
Re: Gender Table Help
« Reply #1 on: October 15, 2006, 09:44:38 PM »
What are the fields named in the gender table?

Its kind of odd to have a whole seperate table just for gender.  That should be part of the main player accounts table I would think...

Offline Pug307

  • Level 21
  • *
  • Posts: 244
  • Reputation: +6/-1
    • View Profile
Re: Gender Table Help
« Reply #2 on: October 15, 2006, 09:47:43 PM »
In more detail. The main table has a tab called user. I added a tab inside user called gender and the register form submits into it either Male or Female which is selcted using radio buttons.

I have now figured that I should have used 2 seperate tabs, male and female and I could have used the count row or whatever it is.

I am just trying to figure a way of seperating male and female without changing the table and register.php



Offline dvd871

  • Level 21
  • *
  • Posts: 238
  • Reputation: +7/-0
    • View Profile
    • Dominion Siege
Re: Gender Table Help
« Reply #3 on: October 15, 2006, 09:57:21 PM »
Ok so is it something like male = 1 and female = 2?  or how is the male vs. female data stored?

Offline Pug307

  • Level 21
  • *
  • Posts: 244
  • Reputation: +6/-1
    • View Profile
Re: Gender Table Help
« Reply #4 on: October 15, 2006, 10:25:10 PM »
In the table under user> gender it stores the data as male and female.



Offline dvd871

  • Level 21
  • *
  • Posts: 238
  • Reputation: +7/-0
    • View Profile
    • Dominion Siege
Re: Gender Table Help
« Reply #5 on: October 15, 2006, 10:32:38 PM »
Ok so it would be something like this:

$sql = mysql_query("SELECT COUNT(*) as male FROM user WHERE gender="MALE") or die("Error: ".mysql_error());
$row = mysql_fetch_assoc($sql);
$male = $row['male'];

The $male var should have the count of male players from the database.

$sql = mysql_query("SELECT COUNT(*) as female FROM user WHERE gender="FEMALE") or die("Error: ".mysql_error());
$row = mysql_fetch_assoc($sql);
$female = $row['female'];

The $female var should have the count of female players from the database.

This is very rough code though, and I am simply guessing at the table names and data.  If I had the sql file for the player accounts or gender, I could tell you exactly what the code is.

Cheers,
Dan


Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Gender Table Help
« Reply #6 on: October 16, 2006, 12:58:05 PM »
I don't think it's so useful to have a seperate table for gender... I'm not sure what you mean by 'tabs' though... Do you mean columns?
`user` table contains the `gender` column, along with the username, password, email columns, etc.?

dvd871's code should work fine anyways :)

Offline Pug307

  • Level 21
  • *
  • Posts: 244
  • Reputation: +6/-1
    • View Profile
Re: Gender Table Help
« Reply #7 on: October 16, 2006, 03:11:52 PM »
Yes it will be columns then. I will give it a go and let you know of the outcome. Hopefully it will work for me  ;D

Nope don't work, just throws an SQL Error
« Last Edit: October 16, 2006, 03:18:30 PM by Pug307 »



Offline dvd871

  • Level 21
  • *
  • Posts: 238
  • Reputation: +7/-0
    • View Profile
    • Dominion Siege
Re: Gender Table Help
« Reply #8 on: October 16, 2006, 04:58:55 PM »
Do you use phpMyadmin?  If you do then go to the table that has the gender info in it.  Do an export of the structure and post that here so we can see what we are dealing with.  It also helps to post what the error messages are.

Offline Pug307

  • Level 21
  • *
  • Posts: 244
  • Reputation: +6/-1
    • View Profile
Re: Gender Table Help
« Reply #9 on: October 16, 2006, 06:56:26 PM »
`gender` varchar(20) character set latin1 collate latin1_general_ci NOT NULL default '',

This piece of code below is in the signup.php form

<td bgcolor="#003366">Gender:</td><td bgcolor="#333333">&nbsp;Male:<input type="radio" value="Male" name="gender"> &nbsp;Female:<input type="radio" value="Female" name="gender"></td>

An example of the attacking status would be

Attacking:</font></span> <?=$attacking?> - this is where it displays how many members are attacking

$getattacking = fetch("SELECT COUNT(id) FROM $tab[pimp] WHERE lastattacked>$idle AND rid='$game[round]';");
  if (!isset($attacking)) $attacking=0;

  $attacking = $attacking+$getattacking;

I hope this makes some sense to you

Thanks

« Last Edit: October 16, 2006, 06:59:08 PM by Pug307 »



Offline dvd871

  • Level 21
  • *
  • Posts: 238
  • Reputation: +7/-0
    • View Profile
    • Dominion Siege
Re: Gender Table Help
« Reply #10 on: October 17, 2006, 01:19:55 AM »
`gender` varchar(20) character set latin1 collate latin1_general_ci NOT NULL default '',

This piece of code below is in the signup.php form

<td bgcolor="#003366">Gender:</td><td bgcolor="#333333">&nbsp;Male:<input type="radio" value="Male" name="gender"> &nbsp;Female:<input type="radio" value="Female" name="gender"></td>

An example of the attacking status would be

Attacking:</font></span> <?=$attacking?> - this is where it displays how many members are attacking

$getattacking = fetch("SELECT COUNT(id) FROM $tab[pimp] WHERE lastattacked>$idle AND rid='$game[round]';");
  if (!isset($attacking)) $attacking=0;

  $attacking = $attacking+$getattacking;

I hope this makes some sense to you

Thanks

// for male users
$msql = fetch("SELECT COUNT(*) as count FROM users WHERE gender='Male';") or die("Error: ".mysql_error());
$row = mysql_fetch_assoc($msql);
$male = $row['count'];

// for female users
$fsql = fetch("SELECT COUNT(*) as count FROM users WHERE gender='Female';") or die("Error: ".mysql_error());
$row = mysql_fetch_assoc($fsql);
$female = $row['count'];

Should work looking at the info above.
« Last Edit: October 17, 2006, 01:21:40 AM by dvd871 »

Offline Mgccl

  • Level 7
  • *
  • Posts: 33
  • Reputation: +1/-0
    • View Profile
    • WebDevLogs
Re: Gender Table Help
« Reply #11 on: November 09, 2006, 10:24:35 PM »
say... it is a waste to use varchar(20) for gender...
I will use int(1)
where 0,1,2 are used...
0 = not set
1 = male
2 = female
It's much less data in that way... and supposeablely faster...

Offline Marek

  • Level 18
  • *
  • Posts: 177
  • Reputation: +7/-0
  • XHTML, CSS, JS, PHP and MySQL are my pantheon.
    • View Profile
Re: Gender Table Help
« Reply #12 on: December 13, 2006, 08:06:47 AM »
Or even better, use an ENUM column. It stores the data as 0, 1, etc but it still lets you do a query with the corresponding string value, e.g.: SELECT * WHERE `gender` = 'male'

You give MySQL the possible values when creaitng the table: ENUM('male', 'female') and since it knows how many possibilities there are (in this case, two) it  can store it most efficiently, with the least bytes.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal