average variables are rounding up
02-25-2010, 11:26 AM
|
average variables are rounding up
|
Posts: 611
|
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
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>
|
|
|
|
02-25-2010, 01:00 PM
|
Re: average variables are rounding up
|
Posts: 335
Name: Jerry
|
the number is not rounding up... your if statement is
PHP Code:
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> "; }
notice one A and three B is equal 17 - divide that by 4 is 4.25 which in your equation is landed on A.
you have two options, either break down your if into 0.5 conditions or to make this actually more accurate you could use A- B+ B B- type thing...
5 = A+
4.66 = A
4.33 = A-
4 = B+
3.66 = B
3.33 = B-
...
|
|
|
|
02-25-2010, 01:38 PM
|
Re: average variables are rounding up
|
Posts: 611
|
awesome thanks!!! you are the only person on 2 websites that figured that out for me. could you please show me one little snippet or line of what A should look like in code? please. then i can just do the rest of the code for it based on that. sorry , im desperate.
edit: when i do
Code:
<0.5 && >=1.0;
<1.0 && >=1.5
and keep going i only get up to 2.0 i think for A
edit
is this correct ?
Code:
else if ($average > 4 && $average <= 4.33)
{
$grade = "B+";
echo "You have given ".$employer ." a grade of <strong>B.</strong> ";
}
else if ($average > 4.33 && $average <= 4.66)
{
$grade = "A-";
echo "You have given ".$employer ." a grade of <strong>A-.</strong> ";
}
}
else if ($average > 4.66 && $average <= 5)
{
$grade = "A";
echo "You have given ".$employer ." a grade of <strong>A.</strong> ";
}
else if ($average > 4.33 && $average <= 4.66)
{
$grade = "A+";
echo "You have given ".$employer ." a grade of <strong>A.</strong> ";
}
Last edited by silverglade; 02-25-2010 at 01:53 PM..
|
|
|
|
02-26-2010, 11:04 AM
|
Re: average variables are rounding up
|
Posts: 41
Name: Adam B
|
Think he meant something more like:
Code:
if ($average >= 4.5)
{
// A
}
elseif ($average < 4.5 && $average >= 3.5)
{
// B
}
//......
That's probably about as accurate as you can get it, without re-working the numbering system or adding in -/+, which I think the user would think looked a bit OTT.
|
|
|
|
02-26-2010, 01:52 PM
|
Re: average variables are rounding up
|
Posts: 335
Name: Jerry
|
hmmm adam has got a point there... it maybe confusing for users. May want to consider going back to single characters... sorry for the confusion...
|
|
|
|
02-28-2010, 12:06 PM
|
Re: average variables are rounding up
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
Check this out as an alternative means of solving the problem:
PHP 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) { /* Taking advantage of the fact that the ASCII codes for A-E are 65-69, we can round the final grade and subtract the value from the ASCII code Then, if the result is E (i.e. ASCII 69), replace it with an F */ return ($letter_grade = chr(69 - round($final_grade))) == 69?'F':$letter_grade; }
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'])) { $grade = grade($average); echo "You have given ".$employer ." a grade of <strong>".$grade.".</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
$final_grade_letter = grade($final_grade); echo $userinfo['employer'] ." has an average grade of <strong>".$final_grade_letter.".</strong> ". $userinfo['votes'] . " people voted.";
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>
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
|
« Reply to average variables are rounding up
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|