Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Issue calling and sorting data from phpmyadmin

    I have a table that compiles survey data from people, and I want to be able to sort that data by gender. The survey asks them their gender, and I try to return the average of the "totalEA" field, but can't get it to work correctly. Thanks for any help you can provide.

    Background info: The titles of "gender" and "totalEA" are both columns from the database. Here is a link of what I was trying to follow as well.http://www.tizag.com/mysqlTutorial/mysqlavg.php

    This is the code that I can't get to work:

    Code:
    $queryfemale = "SELECT gender, AVG(totalEA) AS TotalFemale FROM $db_table GROUP BY gender";
    $resultfemale = mysql_query($queryfemale);
    	printf("The totalEA for females is: ");
    	while ($rowfemale = mysql_fetch_assoc($resultfemale)){
    	printf(round($rowfemale['type'], 2));
    	    printf("   ");
    This code returns the totalEA value, not sorted by gender and works correctly, so I tried to model the above code off of it with no success.
    Code:
    $querytest = "SELECT totalEA AS Total FROM $db_table";
    $resulttest = mysql_query($querytest);
    	printf("The results for the European-American section are: ");
    while ($rowtest = mysql_fetch_assoc($resulttest)){
        printf(round($rowtest['Total'], 2));
        printf("   ");
    }

  • #2
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    New issue:

    I got my code somewhat working, so here's the updated version.

    Code:
    $queryfemale = "SELECT totalEA AS TotalFemale FROM $db_table GROUP BY gender";
    $resultfemale = mysql_query($queryfemale);
    	while ($rowfemale = mysql_fetch_assoc($resultfemale)){
    	printf("The totalEA for females is: ");
    	printf(round($rowfemale['TotalFemale'], 2));
    	    printf("and ");
    }
    Now, it is sorting by gender like I want, and returning both the female and male values from this table. However, I can't figure out how to word the "While" part of the code. Currently it repeats itself, giving this result:

    The totalEA for females is: 0 The totalEA for females is: 28

    I want the second sentence to read "male" instead of "female", but I don't know how to change that within the while statement while still outputting both the male and female totals. Any help?


  •  

    Posting Permissions

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