silverglade Posted February 25, 2010 Share Posted February 25, 2010 hi, i have code that takes in user input of grades A-F of an employer. when 2 users submit the grades, the app is supposed to average the grades of the 2 users' input, but instead it rounds the grades up for some reason like this user 1 input stability A honesty F courtesy F loyalty F user input 2 stability C honesty C courtesy C loyalty C and when i search for the employer using the search option, it outputs this Mcdonalds has an average grade of A stability A honesty B courtesy B loyalty B i dont know if that makes sense , any help GREATLY appreciated . thank you. derek here is the code <?php //include("connect1.php"); $server = 'localhost'; $user = 'root'; $password = ''; $database = 'database'; $link = mysql_connect($server, $user, $password); mysql_select_db($database,$link); error_reporting(E_ALL); //error reporting php function //////////////////////////////////////// //////////////////////////////////////// // escape username and password for use in SQL//person said on board "looks fine" like this //to prevent sql injections // This function will prevent errors from other servers that will throw // error when you acces variables that are not yet set. // We first check if the variable exist, in case it exist return the variable // else return empty space function post($fieldname = '', $default = '') { return (isset($_POST[$fieldname])) ? $_POST[$fieldname] : $default; } // I created a function to return a letter to a specific grade function grade($final_grade) { if ($final_grade <= 1) $value = 'F'; elseif ($final_grade > 1 && $final_grade <= 2) $value = 'D'; elseif ($final_grade > 2 && $final_grade <= 3) $value = 'C'; elseif ($final_grade > 3 && $final_grade <= 4) $value = 'B'; elseif ($final_grade > 4) $value = 'A'; return $value; } if($_POST) { //If you want to disable multiple voting of a user to the same company, set to true $check_user = FALSE; $employer = post('employer'); $zip = post('zip'); // We are typecasting int variable, this will make sure that this fields are int type // This also prevent sql injection. Use this only if you are expecting int variables $courtesy = (int) post('courtesy',1); $loyalty = (int) post('loyalty',1); $stability = (int) post('stability',1); $attitude = (int) post('attitude',1); $employer = strtolower(mysql_real_escape_string($employer)); $zip = mysql_real_escape_string($zip); $find = post('find'); $find = mysql_real_escape_string($find); $total = 4; $sum = $courtesy + $loyalty + $stability + $attitude; $average = $sum/$total; } if(isset($_POST['Vote'])) { if ($average <= 1) { $grade = "F"; echo "You have given ".$employer ." a grade of <strong>F.</strong> "; } elseif($average > 1 && $average <= 2) { $grade = "D"; echo "You have given ".$employer ." a grade of <strong>D.</strong> "; } else if ($average > 2 && $average <= 3) { $grade = "C"; echo "You have given ".$employer ." a grade of <strong>C.</strong> "; } else if ($average > 3 && $average <= 4) { $grade = "B"; echo "You have given ".$employer ." a grade of <strong>B.</strong> "; } else if ($average > 4 && $average <= 5) { $grade = "A"; echo "You have given ".$employer ." a grade of <strong>A.</strong> "; } echo '<br /><br />Courtesy: '.grade($courtesy).'<br />'; echo 'Stability: '.grade($stability).'<br />'; echo 'Loyalty: '.grade($loyalty).'<br />'; echo 'Attitude: '.grade($attitude).'<br />'; ///you need to know all the vote values, then divide them by the total, to get average ///where do i store all the vote values? you have to store all the votes for each specific friend. but how? then i have to be able to update the grade based on all the averages. /*Quick suggestion without too much thought, would be to have all the "grades" in a separate table and join them on the userid. id - numeric - [record number] uid - numeric [FK for user table] type - char - [code to identify which grade] value - numeric - [value for the grading] vid - numeric - [id of voter] that covers another of your questions. How to stop fake voting.*/ $process_vote = TRUE; //This is the simplest method for getting a users ip $ip = $_SERVER['REMOTE_ADDR']; if($check_user) { //Check wether the user has already voted for the same company $sql = "SELECT * FROM voters WHERE ip='{$ip}' AND zip='{$zip}' AND employer='{$employer}'"; $query = mysql_query($sql); //If the user has already voted for the same employer, set $process_vote to false if(mysql_num_rows($query)>0) { $process_vote = FALSE; } } if($process_vote) { //Insert vote data to database mysql_query("INSERT INTO friendgrade (grade, employer, zip, courtesy, stability, loyalty, attitude, votes) VALUES('$grade','$employer', '$zip' , '$courtesy',' $stability', '$loyalty','$attitude','1' ) ON DUPLICATE KEY UPDATE courtesy = courtesy + $courtesy, stability = stability + $stability, loyalty = loyalty + $loyalty, attitude = attitude + $attitude, votes = votes + 1"); //Insert user data to database mysql_query("INSERT INTO voters (ip, zip, employer) VALUES('{$ip}','{$zip}','{$employer}')"); } else { echo '<br />You have already voted for this employer!<br />'; } } //output the friend's row into an array and average all rows of his attributes. if(isset($_POST['submit'])) { $query="SELECT employer, loyalty, courtesy, stability, attitude , votes FROM friendgrade WHERE employer = '$find'"; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { $userinfo = mysql_fetch_array($result); //put friends row into an array $userinfo $votes = $userinfo['votes']; $final_grade= ($userinfo['courtesy']+$userinfo['stability']+$userinfo['loyalty']+$userinfo['attitude'])/4; //compute final grade //average the SQL output friend's total grade for all users input if ($final_grade <= 1) { echo $userinfo['employer'] ." has an average grade of <strong>F.</strong> ". $userinfo['votes'] . " people voted."; } elseif ($final_grade > 1 && $final_grade <= 2) { echo $userinfo['employer'] ." has an average grade of <strong>D.</strong> ". $userinfo['votes'] . " people voted."; } elseif ($final_grade > 2 && $final_grade <= 3) { echo $userinfo['employer']." has an average grade of <strong>C.</strong> ". $userinfo['votes'] . " people voted."; } else if ($final_grade > 3 && $final_grade <= 4) { echo $userinfo['employer']." has an average grade of <strong>B.</strong> ". $userinfo['votes'] . " people voted."; } else if ($final_grade > 4 && $final_grade <= 5) { echo $userinfo['employer']." has an average grade of <strong>A.</strong> ". $userinfo['votes'] . " people voted."; } else { echo "Odd results ".$userinfo['employer']." $final_grade ".$userinfo['courtesy']." ".$userinfo['stability']." ".$userinfo['loyalty']." ".$userinfo['attitude']."<br />"; } echo '<br /><br />Courtesy: '.grade($userinfo['courtesy']).'<br />'; echo 'Stability: '.grade($userinfo['stability']).'<br />'; echo 'Loyalty: '.grade($userinfo['loyalty']).'<br />'; echo 'Attitude: '.grade($userinfo['attitude']).'<br />'; }//end if }//end isset ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Update Statement</title> <style type="text/css"> <!-- .style1 {color: #990000} .style2 {color: #0066FF} .style3 {color: #0000CC} .style4 {color: #993399} --> </style> </head> <body> <div align="center">WELCOME TO THE BOSS GRADER</div> <p>Please enter an employer and their zip code, then rate them with the following criteria.<span class="style4"></span><br /></p> <form id="form1" name="form1" method="post" action=""> <table border="0"> <tr> <td>Employer</td> <td> <input name="employer" type="text" id="employer" /></td> </tr> <tr> <td>zip code</td> <td> <input name="zip" type="text" id="zip" /> </td> </tr> <tr> <td>courtesy</td> <td><table> <tr> <td> <input type="radio" name="courtesy" value="1" id="RadioGroup1_0" /> very poor <input type="radio" name="courtesy" value="2" id="RadioGroup1_1" /> poor <input type="radio" name="courtesy" value="3" id="RadioGroup1_2" /> ok <input type="radio" name="courtesy" value="4" id="RadioGroup1_3" /> good <input type="radio" name="courtesy" value="5" id="RadioGroup1_4" /> excellent </td> </tr> </table></td> </tr> <tr> <td>stability</td> <td> <input type="radio" name="stability" id="very_poor3" value="1" /> very poor <input type="radio" name="stability" id="poor3" value="2" /> poor <input type="radio" name="stability" id="ok3" value="3" /> ok <input type="radio" name="stability" id="good3" value="4" /> good <input type="radio" name="stability" id="excellent3" value="5" /> excellent </td> </tr> <tr> <td>loyalty</td> <td><input type="radio" name="loyalty" id="very_poor4" value="1" /> very poor <input type="radio" name="loyalty" id="poor4" value="2" /> poor <input type="radio" name="loyalty" id="ok4" value="3" /> ok <input type="radio" name="loyalty" id="good4" value="4" /> good <input type="radio" name="loyalty" id="excellent4" value="5" /> excellent </td> </tr> <tr> <td>attitude</td> <td><input type="radio" name="attitude" id="very_poor5" value="1" /> very poor <input type="radio" name="attitude" id="poor5" value="2" /> poor <input type="radio" name="attitude" id="ok5" value="3" /> ok <input type="radio" name="attitude" id="good5" value="4" /> good <input type="radio" name="attitude" id="excellent5" value="5" /> excellent </td> </tr> <tr> <td> </td> <td> <input type="submit" name="Vote" value="Submit" /> </td> </tr> </table> </form> <form id="form2" name="form2" method="post" action=""> <table width="430" border="1"> <tr> <td>Enter your Employer's zip code <input type="text" name="zipcode" id="zipcode" /></td> </tr> <tr> <td width="420"> Search for an employer/boss <input type="text" name="find" id="find" /> </td> </tr> <tr> <td><input type="submit" name="submit" id="submit" value="submit" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/193362-average-variables-are-rounding-up/ Share on other sites More sharing options...
MatthewJ Posted February 25, 2010 Share Posted February 25, 2010 Took a quick glance, but I'm sort of confused by this part of the code... $query="SELECT employer, loyalty, courtesy, stability, attitude , votes FROM friendgrade WHERE employer = '$find'"; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { $userinfo = mysql_fetch_array($result); //put friends row into an array $userinfo $votes = $userinfo['votes']; $final_grade= ($userinfo['courtesy']+$userinfo['stability']+$userinfo['loyalty']+$userinfo['attitude'])/4; //compute final grade Shouldn't this be divided by the number of votes and not by 4? Link to comment https://forums.phpfreaks.com/topic/193362-average-variables-are-rounding-up/#findComment-1018081 Share on other sites More sharing options...
silverglade Posted February 25, 2010 Author Share Posted February 25, 2010 thanks, i changed the "/4" to "/$votes", and i got a "Odd Results...error". here is the line i changed $final_grade= ($userinfo['courtesy']+$userinfo['stability']+$userinfo['loyalty']+$userinfo['attitude'])/$votes; //compute final grade Link to comment https://forums.phpfreaks.com/topic/193362-average-variables-are-rounding-up/#findComment-1018107 Share on other sites More sharing options...
MatthewJ Posted February 25, 2010 Share Posted February 25, 2010 Yeah, now that I look... I think I was misunderstanding how your data is stored. Are you incrementing a point total in those fields? so if I add a courtesy vote of "D" for someone, you update their courtesy column and add the points from my vote, and add one to votes as well? If so, I think division by 4 is correct and the problem must lie elsewhere. Back to poking around Link to comment https://forums.phpfreaks.com/topic/193362-average-variables-are-rounding-up/#findComment-1018111 Share on other sites More sharing options...
silverglade Posted February 25, 2010 Author Share Posted February 25, 2010 im not sure i just know that i want to code it so that when 2 people vote, like this, both "stability" "honesty" etc attributes are averaged between voters , should be like this user input 1 F A F F user input 2 C C C C and then i do a search for the employer, and it should average all the voters votes. like this D B D D but instead i get something like this B A B B it averages the votes up, or messed up somehow. i cant find in the code what is wrong. any more help GREATLY appreciated, ive wanted to make this a reality for a month. hehe Link to comment https://forums.phpfreaks.com/topic/193362-average-variables-are-rounding-up/#findComment-1018116 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.