kickstart Posted January 20, 2010 Share Posted January 20, 2010 Hi I would be tempted to add an else to output $final_grade, $userinfo[1], $userinfo[2], $userinfo[3] and $userinfo[4] if $final_grade doesn't match one of the expected values. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/188890-incrementing-by-number-a-row-with-a-specific-user-name-in-a-table-row/page/3/#findComment-998740 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 thanks keith, i dont know how to do that though or what you mean. is that to debug? im still getting (blank) has a grade of A. i have no idea why though. once i get that initial output finished, im pretty sure ill be able to average and output the overall statistic grades for that friend. then i have to figure out how to search also by zip code added to friend. then the application will be finished! yay. any more help majorly appreciated. thanks. derek here is the new full code. <?php include("connect1.php"); 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 $field1 = mysql_real_escape_string($field1); $field2 = mysql_real_escape_string($field2); $field3 = mysql_real_escape_string($field3); /// query db and loop through rows example $field1 = $_POST['randomnumber']; $field2 = $_POST['friend']; $field3 = $_POST['zip']; $field4 = $_POST['grade1']; $field5 = $_POST['grade2']; $field6 = $_POST['grade3']; $field7 = $_POST['grade4']; $find = $_POST['find']; $field4 = (int)$field4; $field5 = (int)$field5; $field6 = (int)$field6; $field7 = (int)$field7; $total = 4; $sum = $field4 + $field5 + $field6 + $field7; $average = $sum/$total; if(isset($_POST['Submit'])){ if ($average <= 1) { $grade = "F"; echo "You have given $field2 a grade of <strong>F.</strong> "; } else if ($average > 1 && $average <= 2) { $grade = "D"; echo "You have given $field2 a grade of <strong>D.</strong> "; } else if ($average > 2 && $average <= 3) { $grade = "C"; echo "You have given $field2 a grade of <strong>C.</strong> "; } else if ($average > 3 && $average <= 4) { $grade = "B"; echo "You have given $field2 a grade of <strong>B.</strong> "; } else if ($average > 4 && $average <= 5) { $grade = "A"; echo "You have given $field2 a grade of <strong>A.</strong> "; } ///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.*/ mysql_query("INSERT INTO friendgrade (randomnumber, grade, zip, friend, courtesy,stability,loyalty,attitude) VALUES('$field1','$grade' , '$field3','$field2', $field4 , '$field5',' $field6', '$field7' ) ON DUPLICATE KEY UPDATE courtesy = courtesy + $field4, stability = stability + $field5, loyalty = loyalty + $field6, attitude = attitude + $field7"); }//end isset //output the friend's row into an array and average all rows of his attributes. if(isset($_POST['submit'])){ $query="SELECT friend, loyalty, courtesy, stability, attitude FROM friendgrade WHERE friend = '$find'"; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { $userinfo = mysql_fetch_array($result); //put friends row into an array $userinfo $final_grade= ($userinfo[1]+$userinfo[2]+$userinfo[3]+$userinfo[4])/4; //compute final grade //average the SQL output friend's total grade for all users input if ($final_grade <= 1) { echo $final_grade[0]." has an average grade of <strong>F.</strong> "; } else if ($final_grade > 1 && $final_grade <= 2) { echo $final_grade[0]." has an average grade of <strong>D.</strong> "; } else if ($final_grade > 2 && $final_grade <= 3) { echo $final_grade[0]." has an average grade of <strong>C.</strong> "; } else if ($final_grade > 3 && $final_grade <= 4) { echo $final_grade[0]." has an average grade of <strong>B.</strong> "; } else if ($final_grade > 4 && $final_grade <= 5) { echo $final_grade[0]." has an average grade of <strong>A.</strong> "; } }//end if else { echo "Friend not found"; //add this after found to get errors or see whats happening $query" } }//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> </p> <div align="center">WELCOME TO THE FRIENDGRADER</div> <p>Please enter a friend, their zip code, and 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>Enter a random number</td> <td> <input name="randomnumber" type="text" id="randomnumber" /></td> </tr> <tr> <td>Friend</td> <td> <input name="friend" type="text" id="friend" /></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="grade1" value="1" id="RadioGroup1_0" /> very poor <input type="radio" name="grade1" value="2" id="RadioGroup1_1" /> poor <input type="radio" name="grade1" value="3" id="RadioGroup1_2" /> ok <input type="radio" name="grade1" value="4" id="RadioGroup1_3" /> good <input type="radio" name="grade1" value="5" id="RadioGroup1_4" /> excellent </td> </tr> </table> </td> </tr> <tr> <td>stability</td> <td><input type="radio" name="grade2" id="very_poor3" value="1" /> very poor <input type="radio" name="grade2" id="poor3" value="2" /> poor <input type="radio" name="grade2" id="ok3" value="3" /> ok <input type="radio" name="grade2" id="good3" value="4" /> good <input type="radio" name="grade2" id="excellent3" value="5" /> excellent</td> </tr> <tr> <td>loyalty</td> <td><input type="radio" name="grade3" id="very_poor4" value="1" /> very poor <input type="radio" name="grade3" id="poor4" value="2" /> poor <input type="radio" name="grade3" id="ok4" value="3" /> ok <input type="radio" name="grade3" id="good4" value="4" /> good <input type="radio" name="grade3" id="excellent4" value="5" /> excellent</td> </tr> <tr> <td>attitude</td> <td><input type="radio" name="grade4" id="very_poor5" value="1" /> very poor <input type="radio" name="grade4" id="poor5" value="2" /> poor <input type="radio" name="grade4" id="ok5" value="3" /> ok <input type="radio" name="grade4" id="good5" value="4" /> good <input type="radio" name="grade4" id="excellent5" value="5" /> excellent</td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> <form id="form2" name="form2" method="post" action=""> <table width="335" border="1"> <tr> <td width="325"> search friends <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> Quote Link to comment https://forums.phpfreaks.com/topic/188890-incrementing-by-number-a-row-with-a-specific-user-name-in-a-table-row/page/3/#findComment-998746 Share on other sites More sharing options...
kickstart Posted January 20, 2010 Share Posted January 20, 2010 Hi Something like this:- if(mysql_num_rows($result) > 0) { $userinfo = mysql_fetch_array($result); //put friends row into an array $userinfo $final_grade= ($userinfo[1]+$userinfo[2]+$userinfo[3]+$userinfo[4])/4; //compute final grade //average the SQL output friend's total grade for all users input if ($final_grade <= 1) { echo $userinfo[0]." has an average grade of <strong>F.</strong> "; } else if ($final_grade > 1 && $final_grade <= 2) { echo $userinfo[0]." has an average grade of <strong>D.</strong> "; } else if ($final_grade > 2 && $final_grade <= 3) { echo $userinfo[0]." has an average grade of <strong>C.</strong> "; } else if ($final_grade > 3 && $final_grade <= 4) { echo $userinfo[0]." has an average grade of <strong>B.</strong> "; } else if ($final_grade > 4 && $final_grade <= 5) { echo $userinfo[0]." has an average grade of <strong>A.</strong> "; } else { echo "Odd results ".$userinfo[0]." $final_grade ".$userinfo[1]." ".$userinfo[2]." ".$userinfo[3]." ".$userinfo[4]."<br />"; } }//end if All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/188890-incrementing-by-number-a-row-with-a-specific-user-name-in-a-table-row/page/3/#findComment-998758 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 thanks again. the else message doesnt show up. it just says "has an average grade of A" sorry about this. here is the link to the page if you want to try it out. http://oddnerdrum.info/friendgrade6.php derek here is the new code for that area //output the friend's row into an array and average all rows of his attributes. if(isset($_POST['submit'])){ $query="SELECT friend, loyalty, courtesy, stability, attitude FROM friendgrade WHERE friend = '$find'"; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { $userinfo = mysql_fetch_array($result); //put friends row into an array $userinfo $final_grade= ($userinfo[1]+$userinfo[2]+$userinfo[3]+$userinfo[4])/4; //compute final grade //average the SQL output friend's total grade for all users input if ($final_grade <= 1) { echo $final_grade[0]." has an average grade of <strong>F.</strong> "; } else if ($final_grade > 1 && $final_grade <= 2) { echo $final_grade[0]." has an average grade of <strong>D.</strong> "; } else if ($final_grade > 2 && $final_grade <= 3) { echo $final_grade[0]." has an average grade of <strong>C.</strong> "; } else if ($final_grade > 3 && $final_grade <= 4) { echo $final_grade[0]." has an average grade of <strong>B.</strong> "; } else if ($final_grade > 4 && $final_grade <= 5) { echo $final_grade[0]." has an average grade of <strong>A.</strong> "; } else { echo "Odd results ".$userinfo[0]." $final_grade ".$userinfo[1]." ".$userinfo[2]." ".$userinfo[3]." ".$userinfo[4]."<br />"; } }//end if }//end isset Quote Link to comment https://forums.phpfreaks.com/topic/188890-incrementing-by-number-a-row-with-a-specific-user-name-in-a-table-row/page/3/#findComment-998763 Share on other sites More sharing options...
kickstart Posted January 20, 2010 Share Posted January 20, 2010 Hi You have missed the other change. $final_grade[0] needs to be $userinfo[0] to get the name. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/188890-incrementing-by-number-a-row-with-a-specific-user-name-in-a-table-row/page/3/#findComment-998766 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 HAHA you are awesome! now it says "derek has an average grade of A" but for the rows i put all 5's for the first row, and all 1's for the second 'derek' row. but it says , instead of "derek has an average grade of C" it says "derek has an average grade of A", its ignoring the average i think or doing it wrong. any more help greatly appreciated. derek Quote Link to comment https://forums.phpfreaks.com/topic/188890-incrementing-by-number-a-row-with-a-specific-user-name-in-a-table-row/page/3/#findComment-998772 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 i got ambitious and tried to add a vote number of people who voted after the "derek has an average grade of A" so it should be "Derek has an average grade of A" 10 people voted. but now all i have is a blank screen, neither the average nor the vote works. any more help GREATLY appreciated! here is the new code. <?php include("connect1.php"); 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 $field2 = mysql_real_escape_string($field2); $field3 = mysql_real_escape_string($field3); /// query db and loop through rows example $field2 = $_POST['friend']; $field3 = $_POST['zip']; $field4 = $_POST['grade1']; $field5 = $_POST['grade2']; $field6 = $_POST['grade3']; $field7 = $_POST['grade4']; $find = $_POST['find']; $field4 = (int)$field4; $field5 = (int)$field5; $field6 = (int)$field6; $field7 = (int)$field7; $total = 4; $sum = $field4 + $field5 + $field6 + $field7; $average = $sum/$total; if(isset($_POST['Submit'])){ $votes= votes++; //increment vote by one if ($average <= 1) { $grade = "F"; echo "You have given $field2 a grade of <strong>F.</strong> "; } else if ($average > 1 && $average <= 2) { $grade = "D"; echo "You have given $field2 a grade of <strong>D.</strong> "; } else if ($average > 2 && $average <= 3) { $grade = "C"; echo "You have given $field2 a grade of <strong>C.</strong> "; } else if ($average > 3 && $average <= 4) { $grade = "B"; echo "You have given $field2 a grade of <strong>B.</strong> "; } else if ($average > 4 && $average <= 5) { $grade = "A"; echo "You have given $field2 a grade of <strong>A.</strong> "; } ///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.*/ mysql_query("INSERT INTO friendgrade (votes, grade, friend, zip,courtesy,stability,loyalty,attitude) VALUES('$votes','$field2','$field3', '$field4' , '$field5',' $field6', '$field7' ) ON DUPLICATE KEY UPDATE courtesy = courtesy + $field4, stability = stability + $field5, loyalty = loyalty + $field6, attitude = attitude + $field7"); }//end isset //output the friend's row into an array and average all rows of his attributes. if(isset($_POST['submit'])){ $query="SELECT friend, loyalty, courtesy, stability, attitude, votes FROM friendgrade WHERE friend = '$find'"; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { $userinfo = mysql_fetch_array($result); //put friends row into an array $userinfo $final_grade= ($userinfo[1]+$userinfo[2]+$userinfo[3]+$userinfo[4])/4; //compute final grade //average the SQL output friend's total grade for all users input if ($final_grade <= 1) { echo $userinfo[0] ." has an average grade of <strong>F.</strong>,". $userinfo[5] . "people voted" ; } else if ($final_grade > 1 && $final_grade <= 2) { echo $userinfo[0] ." has an average grade of <strong>D.</strong> ". $userinfo[5] . "people voted" ; } else if ($final_grade > 2 && $final_grade <= 3) { echo $userinfo[0]." has an average grade of <strong>C.</strong> ". $userinfo[5] . "people voted" ; } else if ($final_grade > 3 && $final_grade <= 4) { echo $userinfo[0]." has an average grade of <strong>B.</strong> ". $userinfo[5] . "people voted" ; } else if ($final_grade > 4 && $final_grade <= 5) { echo $userinfo[0]." has an average grade of <strong>A.</strong> ". $userinfo[5] . "people voted" ; } else { echo "Odd results ".$userinfo[0]." $final_grade ".$userinfo[1]." ".$userinfo[2]." ".$userinfo[3]." ".$userinfo[4]."<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> </p> <div align="center">WELCOME TO THE FRIENDGRADER</div> <p>Please enter a friend, their zip code, and 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>Friend</td> <td> <input name="friend" type="text" id="friend" /></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="grade1" value="1" id="RadioGroup1_0" /> very poor <input type="radio" name="grade1" value="2" id="RadioGroup1_1" /> poor <input type="radio" name="grade1" value="3" id="RadioGroup1_2" /> ok <input type="radio" name="grade1" value="4" id="RadioGroup1_3" /> good <input type="radio" name="grade1" value="5" id="RadioGroup1_4" /> excellent </td> </tr> </table> </td> </tr> <tr> <td>stability</td> <td><input type="radio" name="grade2" id="very_poor3" value="1" /> very poor <input type="radio" name="grade2" id="poor3" value="2" /> poor <input type="radio" name="grade2" id="ok3" value="3" /> ok <input type="radio" name="grade2" id="good3" value="4" /> good <input type="radio" name="grade2" id="excellent3" value="5" /> excellent</td> </tr> <tr> <td>loyalty</td> <td><input type="radio" name="grade3" id="very_poor4" value="1" /> very poor <input type="radio" name="grade3" id="poor4" value="2" /> poor <input type="radio" name="grade3" id="ok4" value="3" /> ok <input type="radio" name="grade3" id="good4" value="4" /> good <input type="radio" name="grade3" id="excellent4" value="5" /> excellent</td> </tr> <tr> <td>attitude</td> <td><input type="radio" name="grade4" id="very_poor5" value="1" /> very poor <input type="radio" name="grade4" id="poor5" value="2" /> poor <input type="radio" name="grade4" id="ok5" value="3" /> ok <input type="radio" name="grade4" id="good5" value="4" /> good <input type="radio" name="grade4" id="excellent5" value="5" /> excellent</td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> <form id="form2" name="form2" method="post" action=""> <table width="335" border="1"> <tr> <td width="325"> search friends <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> Quote Link to comment https://forums.phpfreaks.com/topic/188890-incrementing-by-number-a-row-with-a-specific-user-name-in-a-table-row/page/3/#findComment-998830 Share on other sites More sharing options...
fenway Posted January 20, 2010 Share Posted January 20, 2010 OK, that's quite enough. Keith has provided you with the sql solution -- if you're having issues coding php scripts, this is the wrong board. I've moving it to PHP help. Quote Link to comment https://forums.phpfreaks.com/topic/188890-incrementing-by-number-a-row-with-a-specific-user-name-in-a-table-row/page/3/#findComment-998831 Share on other sites More sharing options...
kickstart Posted January 21, 2010 Share Posted January 21, 2010 HAHA you are awesome! now it says "derek has an average grade of A" but for the rows i put all 5's for the first row, and all 1's for the second 'derek' row. but it says , instead of "derek has an average grade of C" it says "derek has an average grade of A", its ignoring the average i think or doing it wrong. any more help greatly appreciated. derek So you are now having mulitple rows for a person? If so then PLEASE put in a column listing the score from that person. The AVG was taken out of the SELECT because it appeared you had a single row per person (so nothing to AVG). Even better do something like this:- Table of People Id Name Zip Table of Friends Id IdOfPerson IdOfFriend Courtesy Stability Loyalty Attitude All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/188890-incrementing-by-number-a-row-with-a-specific-user-name-in-a-table-row/page/3/#findComment-999313 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.