SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Resistance is Futile webgodjj's Avatar
    Join Date
    Nov 2002
    Location
    Madison, WI USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    results by percent

    Hi, I have a table with various colums that I would like to display some stastisics.

    For example on colum is Gender. It holds either Male or Female.

    How can I output the percentage of each?

    ie... Female = 80% Male = 20%

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, assuming your taking data from the database and note also this is from a poll I did more than a year ago, here is some script which will help you get the proper percentages.

    PHP Code:
    # begin here
    $database mysql_connect('localhost''root');
    mysql_select_db('mydatabase'$database);
    $db_build mysql_query('select * from mypoll'$database);
    .
    .
    # create TABLE - I've left it out since it's not required and I can't be bothered to type in markup 8)
    .
    .
    $vote_total 0;
    while(
    $mytotal mysql_fetch_row($db_build)) {
    $vote_total += $mytotal[2];
    }
     
    # I knows there's no excuse for this 2nd query... though I was learning at the time 8) LoL
    $db_build mysql_query('select * from mypoll'$database);
    while(
    $myrow mysql_fetch_row($db_build)) {
    # the actual calculation that your looking for is here:::::::>>>
    $number round((100/$vote_total)*myrow[2], 0);
    # rounded down to 2 decimal places of course 8)
    # do something with $number before you loop back - put it in an array or something 8)

    I don't have the database structure though the resultant display was like this:

    Colour ---- No. Votes ---- %
    red ---- 55 ---- 24
    blue ---- 77 ---- 33
    green ---- 33 ---- 14
    yellow ---- 66 ---- 29

    Total No. Votes: 231

    Check and you'll see they all add up 8) This should help you since you're only looking for the line I've pointed out in the script - pay attention to those while loops to figure it all out 8)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •