TinyPortal

Development => Block Codes => Topic started by: Max on January 10, 2007, 01:40:36 PM

Title: Total Males/Females Block
Post by: Max on January 10, 2007, 01:40:36 PM
Just wondering if someone would help me out with this block,

I know the SQL would be something like this....

Total Males

SELECT count( gender )
FROM _members
WHERE gender =1


Total Females

SELECT count( gender )
FROM _members
WHERE gender =2


Undecided

SELECT count( gender )
FROM _members
WHERE gender =0


Example Of Block:
Males: 797
Females: 345
Undecided: 103

Edit:

Came up with something but its prob not the best way of doing it hehe...
if anyone knows a better way let me know  ;)


$request = db_query("SELECT count( gender ) FROM smf_members WHERE gender =1", __FILE__, __LINE__);
if (mysql_num_rows($request) > 0)
{
echo '<table border="0" cellpadding="0" cellspacing="2" width="100%">';
while($row = mysql_fetch_assoc($request)){
echo '<b>Total Males:</b> <font color=#1503BD>' . $row['count( gender )'] . '</font><br />';
}
mysql_free_result($request);
  }

$request = db_query("SELECT count( gender ) FROM smf_members WHERE gender =2", __FILE__, __LINE__);
if (mysql_num_rows($request) > 0)
{
while($row = mysql_fetch_assoc($request)){
echo '<b>Total Females:</b> <font color=#E61EFE>' . $row['count( gender )'] . '</font><br/>';
}
mysql_free_result($request);
  }

$request = db_query("SELECT count( gender ) FROM smf_members WHERE gender =0", __FILE__, __LINE__);
if (mysql_num_rows($request) > 0)
{
while($row = mysql_fetch_assoc($request)){
echo '<b>Undecided:</b> <font color=#000000>' . $row['count( gender )'] . '</font>';
}
echo '</table>';
mysql_free_result($request);
  }
Title: Re: Total Males/Females Block
Post by: JPDeni on January 10, 2007, 02:23:42 PM

global $db_prefix;

$result = db_query("
SELECT COUNT(*) AS totalMembers, gender
FROM {$db_prefix}members
GROUP BY gender", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($result))
$gender[$row['gender']] = $row['totalMembers'];

isset($gender[0]) ? null : $gender[0] = 0;
isset($gender[1]) ? null : $gender[1] = 0;
isset($gender[2]) ? null : $gender[2] = 0;

echo "
<b>Males:</b> $gender[1]<br />
<b>Females:</b> $gender[2]<br />
<b>Undecided:</b> $gender[0]<br />
";
Title: Re: Total Males/Females Block
Post by: Max on January 10, 2007, 02:28:47 PM

Thanks JPDeni, i was doing it the loooooooooong way! haha  :up:
Title: Re: Total Males/Females Block
Post by: IchBin on January 10, 2007, 02:33:49 PM
Quote from: Maxâ„¢ on January 10, 2007, 02:28:47 PM

Thanks JPDeni, i was doing it the loooooooooong way! haha  :up:
I can't tell you how many times I do it that way Max lol. Then to see someone else do it with half the code....
Title: Re: Total Males/Females Block
Post by: JPDeni on January 10, 2007, 02:46:46 PM
:) It's just experience. I used to write things the long way around, too, and still do often. But I've come to the point where I look to make as few calls to the database as possible. Sorta like when someone is going to the grocery store. If they're going for milk, might as well have them pick up bread, butter and cheese at the same time. ;)

Also, I cheat. I looked at the code in the Stats.php file.  :2funny:

Title: Re: Total Males/Females Block
Post by: wilsy on January 01, 2008, 10:48:29 AM
Hi all,

Thanks to Max and JPDeni for this excellent snippet.

I add a little piece of code to mine (just a link really) which allows a member to check they have updated their profile with the correct gender.

Quoteglobal $db_prefix, $scripturl, $context;

$result = db_query("
   SELECT COUNT(*) AS totalMembers, gender
   FROM {$db_prefix}members
   GROUP BY gender", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($result))
   $gender[$row['gender']] = $row['totalMembers'];

isset($gender[0]) ? null : $gender[0] = 0;
isset($gender[1]) ? null : $gender[1] = 0;
isset($gender[2]) ? null : $gender[2] = 0;

echo "
Males: $gender[1]

Females: $gender[2]

Undecided: $gender[0]

";

echo '

<a href="', $scripturl ,'?action=profile;u=', $context['user']['id'] ,';sa=forumProfile">Check Yours Now!</a>

';

I have another TP/SMF forum at www.farnworthgrammar.com which was originally a Snitz forum, and as a result none of the 1000+ members had a gender entered. This has been helpful to (slowly) rectify that problem.

What would really take this snippet forward is if the male, female and undecided bullets were hyperlinked to the memberlist, which then listed all the males, females or undecided  ;D

Happy New Year!

Regards,

Wilsy.
Title: Re: Total Males/Females Block
Post by: alhaudhie on January 02, 2008, 01:01:05 AM
how can we make default button before Male,Females,Undecided text?
Title: Re: Total Males/Females Block
Post by: IchBin on January 02, 2008, 01:04:59 AM
Replace or add to this code with your image tag.
<b>Males:</b>

Title: Re: Total Males/Females Block
Post by: alhaudhie on January 02, 2008, 02:11:20 AM
Quote from: IchBinâ,,¢ on January 02, 2008, 01:04:59 AM
Replace or add to this code with your image tag.
<b>Males:</b>



using html?
Title: Re: Total Males/Females Block
Post by: IchBin on January 02, 2008, 07:37:20 AM
Of course! :) Thats the only way you can display an image in SMF unless you use flash or something.
Title: Re: Total Males/Females Block
Post by: alhaudhie on January 02, 2008, 10:48:15 AM
 :up: :laugh:

tq....
Title: Re: Total Males/Females Block
Post by: MrCare on November 30, 2010, 05:52:12 PM
Any UPDATE for latest SMF rc4? Coz all genders counts 0
Title: Re: Total Males/Females Block
Post by: IchBin on November 30, 2010, 07:06:53 PM
I think that all is needed is to change db_query to tpdb_query in the code.
Title: Re: Total Males/Females Block
Post by: MrCare on December 01, 2010, 03:39:59 AM
Hope somebody can make change.
Title: Re: Total Males/Females Block
Post by: IchBin on December 01, 2010, 04:14:38 AM
Quote from: QUBBAH on December 01, 2010, 03:39:59 AM
Hope somebody can make change.

You can't make the change yourself?
Title: Re: Total Males/Females Block
Post by: Freddy on December 01, 2010, 11:43:24 AM
Please make an effort Qubbah.  :o

Did you used to be mrCare on the old forum ?
Title: Re: Total Males/Females Block
Post by: MrCare on December 01, 2010, 03:12:33 PM
 ;D

Im mrcare...

I try to use smfunc database... but i think i dont know soo well..

sorry..

hope somebody can help.
Title: Re: Total Males/Females Block
Post by: IchBin on December 01, 2010, 03:34:36 PM
QUBBAH, I gave you the instructions on how to fix the problem. There is nothing you need to do with smcFunc.
Title: Re: Total Males/Females Block
Post by: WillyP on December 01, 2010, 06:27:11 PM
Might not be too clear to someone who is ESL..

QUBBAH, you have code in the block, look in this code and you will see: db_query

Every time you see that, change it to this:  tpdb_query

Then save the block.
Title: Re: Total Males/Females Block
Post by: MrCare on December 02, 2010, 11:27:24 AM
Yes.. thanks for all...
got it.