rob0225 Posted October 26, 2008 Share Posted October 26, 2008 I can really use some help on this. I am a php virgin and cannot figure out this issue. I need to retrieve data from a mySQL table and display it on a webpage. I have a survey on my website with the results going to a mySQL db. I would like to display the results of that survey. My table has 7 fields and the value can be anyone of 5 different options that the user taking the survey can select. I would like to display the results of each question as a percentage. I.E. 1. Food Quality- Excellent Very Good Good Fair Poor 75% 20% 3% 2% 0% and so on and so on for each of the questions I asked. Help please! Thanks, Robert Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted October 26, 2008 Share Posted October 26, 2008 Robert, I think a bit more info about your database is needed, I have stuff flying through my brain, because I think i can see what you need, but before I give you anything, i would like to know more about the structure of your db so that i dont get you all confused Quote Link to comment Share on other sites More sharing options...
rob0225 Posted October 26, 2008 Author Share Posted October 26, 2008 Lodius, I have 1 table with 10 fields in my database. 7 of which are answers to the questions I asked on my survey. (the other 3 are name, email and comments) Of each of the 7 questions a person has the option of selecting one of 5 answers from a drop down box. I only want to be able to display the combined results of the 7 questions as a percentage. I.E. for question # 1, 50% of the people answered "Excellent", 25% answered "Very Good", 15% answered "Good", 5% answered "Fair" and 5% answered "Poor". The actual percentages will obviously vary based upon the actual answers but was used to give you an idea of what I am trying to accomplish. I would need the code to calculate the percentages based upon the query. Thanks, Robert Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted October 26, 2008 Share Posted October 26, 2008 Divide the number of votes for a particular answer by the total number of votes and multiply by 100, and you'll have the percentage for that particular answer. Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted October 26, 2008 Share Posted October 26, 2008 ok thats what i was envisioning <?php //to get how may people answered what $excellent = mysql_query("SELECT question_1 FROM table WHERE question_1 = 'Excellent'"); $excelllent_count = count(mysql_fetch_assoc($excellent)); //to get total number of responses $total = mysql_query('SELECT question_1 FROM table'); $total_count = count(mysql_fetch_assoc($count)); $result = ($excellent_count/$total_count)*100; printf( "%2d\% of people think question 1 is excellent"); // the \% should be displayed as a % sign, but im tired, might require tweaking ?> that is how to do 1 answer for 1 question, $total of course is reusable for all answers to the same question, there are definately better ways to do this but I think it will work, though untested, oh and it will not round up or down on the percentages, so there is the possibility that your percentages wont add up to 100 i use a dba so using mysql_fetch_assoc might not be necesary but i dont think it will hurt either, guess you could replace count(mysql_fetch_assoc( with mysql_numrows.....Im tired Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.