silverglade Posted January 19, 2010 Author Share Posted January 19, 2010 i changed print_r ($array); to print_r ( $userinfo); oops. and it still doesnt work. 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/2/#findComment-997967 Share on other sites More sharing options...
kickstart Posted January 19, 2010 Share Posted January 19, 2010 Hi  Firstly echo out $query to check the SQL.  Secondly, to get the average is a bit more difficult. Your query at the moment is specifying the name so would ignore any row for a friend of someone else. Ie, if you specified dave then you would get one row and nothing to average it with.  The following would get you a list of all friends of anyone and their average scores:-  SELECT friend,AVG(loyalty), AVG(courtesy), AVG(stability), AVG(attitude) FROM friendgrade GROUP BY friend  However I presume you want only the people who are friends of the name you specify.  A crude way to do this, but easy to understand would be to us the IN clause to check that the friend is a friend of the name you specify.  SELECT friend,AVG(loyalty), AVG(courtesy), AVG(stability), AVG(attitude) FROM friendgrade WHERE friend IN (SELECT friend FROM friendgrade WHERE name='$find') GROUP BY friend  Unfortunatly this is a bit inefficient and also will not bring back the other fields you want (eg grade).  To get round this you can use a JOIN against a subselect. Something like this:-  SELECT a.friend, a.grade, a.zip, a.loyalty, a.courtesy, a.stability, a.attitude, b.avgLoyalty, b.avgCourtesy, b.avgStability, b.avgAttitude FROM friendgrade a INNER JOIN (SELECT friend ,AVG(loyalty) AS avgLoyalty, AVG(courtesy) AS avgCourtesy, AVG(stability) AS avgStability, AVG(attitude) AS avgAttitude FROM friendgrade GROUP BY friend) b ON a.friend = b.friend WHERE name='$find'  That should get you a list of the friends of $find with their grade and zip code and how $find rates them along with that friends average ratings from everyone.  However there is still the issue here that friend could refer to multiple people. Eg, I am sure there are multiple John Smiths in some zip codes. For this reason it would be best to pull the friend name, the person name and the zip codes off into a seperate table and then have friendgrade just use the id fro that other table rather than the name (ie if there were 2 John Smiths in the same zip code then it wouldn't matter as they would be seperate rows on the people table and so seperate numeric IDs).  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/2/#findComment-997971 Share on other sites More sharing options...
silverglade Posted January 19, 2010 Author Share Posted January 19, 2010 oh my god, HAHA. i had no idea a simple rating system would be so hard, thank you so much for all of your help. i will try to implement it. thanks. derek :D :D 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/2/#findComment-997983 Share on other sites More sharing options...
silverglade Posted January 19, 2010 Author Share Posted January 19, 2010 ok here is what i have so far, when i press the submit button for the friend finder, i get "invalid username".  here is the code. again any help you can offfer is greatly appreciated. if youre not too tired that is. hehe.  <?php include("connect1.php"); //////////////////////////////////////// //////////////////////////////////////// // 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['submit']; $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 "$field2 has a grade of <strong>F.</strong> ";  } else if ($average > 1 && $average <= 2) {  $grade = "D";  echo "$field2 has a grade of <strong>D.</strong> "; } else if ($average > 2 && $average <= 3) {  $grade = "C";  echo "$field2 has a grade of <strong>C.</strong> "; } else if ($average > 3 && $average <= 4) {  $grade = "B";  echo "$field2 has a grade of <strong>B.</strong> "; } else if ($average > 4 && $average <= 5) {  $grade = "A";  echo "$field2 has 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      if(isset($_POST['submit'])){  $query="SELECT friend,AVG(loyalty), AVG(courtesy), AVG(stability), AVG(attitude) FROM friendgrade WHERE friend IN (SELECT friend FROM friendgrade WHERE name='$find') GROUP BY friend "; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { $userinfo = mysql_fetch_array($result); print_r($userinfo); } else { echo "Invalid username"; // or whatever  }  }//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>   <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/2/#findComment-997988 Share on other sites More sharing options...
kickstart Posted January 19, 2010 Share Posted January 19, 2010 Hi  The line $find = $_POST['submit']; is just going to assign "submit" to $find. You need to change it back to $find = $_POST['find'];.  Also (for now) change echo "Invalid username"; // or whatever to echo "Invalid username $query"; // or whatever , so at least you can try out the SQL in phpmyadmin.  And I really would recommend against using IN. It was given as an example as it is simple to understand but it is inefficient and does stop you getting other details.  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/2/#findComment-997996 Share on other sites More sharing options...
silverglade Posted January 19, 2010 Author Share Posted January 19, 2010 thank you. ill just try this code and see how it goes for now. but now i get a blank screen. any more help GREATLY appreciated. thanks. derek  <?php include("connect1.php"); //////////////////////////////////////// //////////////////////////////////////// // 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 "$field2 has a grade of <strong>F.</strong> ";  } else if ($average > 1 && $average <= 2) {  $grade = "D";  echo "$field2 has a grade of <strong>D.</strong> "; } else if ($average > 2 && $average <= 3) {  $grade = "C";  echo "$field2 has a grade of <strong>C.</strong> "; } else if ($average > 3 && $average <= 4) {  $grade = "B";  echo "$field2 has a grade of <strong>B.</strong> "; } else if ($average > 4 && $average <= 5) {  $grade = "A";  echo "$field2 has 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      if(isset($_POST['submit'])){  $query="SELECT friend,AVG(loyalty), AVG(courtesy), AVG(stability), AVG(attitude) FROM friendgrade WHERE friend IN (SELECT friend FROM friendgrade WHERE name='$find') GROUP BY friend "; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { $userinfo = mysql_fetch_array($result); print_r($userinfo); } else { echo "Invalid username $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>   <table width="335" border="1">   <tr>    <td width="325"> search friends      <input type="text" name="search" id="search" />  </td>   </tr>   <tr>    <td><input type="submit" name="find" id="find" value="find" /></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/2/#findComment-998000 Share on other sites More sharing options...
kickstart Posted January 19, 2010 Share Posted January 19, 2010 Hi  You are mixing up your form field names  You only go in to find a person if the "submit" button has been pressed. But you have just renamed that to "find", so it will never recognise "submit" being pressed.  Also you have changed the name of the field that contains the person to search for from "find" to "search".  Result is at the moment the name you are trying to search for is going to be the button they pressed, but it will never get into the code to do the search anyway.  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/2/#findComment-998012 Share on other sites More sharing options...
silverglade Posted January 19, 2010 Author Share Posted January 19, 2010 great thanks again! i changed it back to the things you said. but im still getting a blank screen . any more help appreciated. thanks! derek  here is the new code  <?php include("connect1.php"); //////////////////////////////////////// //////////////////////////////////////// // 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 "$field2 has a grade of <strong>F.</strong> ";  } else if ($average > 1 && $average <= 2) {  $grade = "D";  echo "$field2 has a grade of <strong>D.</strong> "; } else if ($average > 2 && $average <= 3) {  $grade = "C";  echo "$field2 has a grade of <strong>C.</strong> "; } else if ($average > 3 && $average <= 4) {  $grade = "B";  echo "$field2 has a grade of <strong>B.</strong> "; } else if ($average > 4 && $average <= 5) {  $grade = "A";  echo "$field2 has 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      if(isset($_POST['submit'])){  $query="SELECT friend,AVG(loyalty), AVG(courtesy), AVG(stability), AVG(attitude) FROM friendgrade WHERE friend IN (SELECT friend FROM friendgrade WHERE name='$find') GROUP BY friend "; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { $userinfo = mysql_fetch_array($result); print_r($userinfo); } else { echo "Invalid username $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>   <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/2/#findComment-998026 Share on other sites More sharing options...
kickstart Posted January 19, 2010 Share Posted January 19, 2010 Hi  You have an "." on the end of the line "$find = $_POST['find'];." which shouldn't be there.  However that should have come up with an error message unless you have error reporting turned off.  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/2/#findComment-998035 Share on other sites More sharing options...
silverglade Posted January 19, 2010 Author Share Posted January 19, 2010 awesome thanks again! ok im getting close! here is the error message i got when i tried to run the new code. at the top of the screen  Invalid username SELECT friend,AVG(loyalty), AVG(courtesy), AVG(stability), AVG(attitude) FROM friendgrade WHERE friend IN (SELECT friend FROM friendgrade WHERE name='finally') GROUP BY friend  and here is the new code  <?php include("connect1.php"); //////////////////////////////////////// //////////////////////////////////////// // 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 "$field2 has a grade of <strong>F.</strong> ";  } else if ($average > 1 && $average <= 2) {  $grade = "D";  echo "$field2 has a grade of <strong>D.</strong> "; } else if ($average > 2 && $average <= 3) {  $grade = "C";  echo "$field2 has a grade of <strong>C.</strong> "; } else if ($average > 3 && $average <= 4) {  $grade = "B";  echo "$field2 has a grade of <strong>B.</strong> "; } else if ($average > 4 && $average <= 5) {  $grade = "A";  echo "$field2 has 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     if(isset($_POST['submit'])){  $query="SELECT friend,AVG(loyalty), AVG(courtesy), AVG(stability), AVG(attitude) FROM friendgrade WHERE friend IN (SELECT friend FROM friendgrade WHERE name='$find') GROUP BY friend "; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { $userinfo = mysql_fetch_array($result); print_r($userinfo); } else { echo "Invalid username $query";  }  }//end issett   ?> <!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/2/#findComment-998049 Share on other sites More sharing options...
kickstart Posted January 19, 2010 Share Posted January 19, 2010 Hi  Do you have a name on the table of "finally"? It would appear that is what has been typed in and sent to this script.  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/2/#findComment-998071 Share on other sites More sharing options...
silverglade Posted January 19, 2010 Author Share Posted January 19, 2010 thanks again for your time. yes i have a friend in the database named "finally". it means maybe i "finally" got it. hehe. so i typed in finally and got the following error. Â Invalid username SELECT friend,AVG(loyalty), AVG(courtesy), AVG(stability), AVG(attitude) FROM friendgrade WHERE friend IN (SELECT friend FROM friendgrade WHERE name='finally') GROUP BY friend 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/2/#findComment-998159 Share on other sites More sharing options...
kickstart Posted January 19, 2010 Share Posted January 19, 2010 Hi  the message you are getting is because no rows were found. The rest is the SQL.  Copy that SQL and paste it into phpmyadmin (or whatever other tool you have to run queries against MySQL) and give it a try.  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/2/#findComment-998317 Share on other sites More sharing options...
silverglade Posted January 19, 2010 Author Share Posted January 19, 2010 thank you! that was very smart Keith. i was using "name" instead of "friend" in the sql. so i fixed it and got the following output on the search. the only problem is it added fields in the array output. it added these non existent elements. [1]=>5.0000 , [2]=>4.0000[3]=>5.0000, [4]=>5.0000 , am i wrong? thanks for helping me. derek   Array ( [0] => finally [friend] => finally [1] => 5.0000 [AVG(loyalty)] => 5.0000 [2] => 4.0000 [AVG(courtesy)] => 4.0000 [3] => 5.0000 [AVG(stability)] => 5.0000 [4] => 5.0000 [AVG(attitude)] => 5.0000 ) 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/2/#findComment-998324 Share on other sites More sharing options...
kickstart Posted January 19, 2010 Share Posted January 19, 2010 Hi  I thought that name was the person whose friends you were looking up while friend was the friend name? Something like that would appear to be the min you would need to calculate the averages.  The reason you get the extra columns back is that mysql_fetch_array() by default brings back the row as BOTH an associative array and a numeric array.  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/2/#findComment-998344 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 keith you are awesome! hahaha. i entered in the table manually 3 rows, friend name "unique" and for the attributes for 2 of them i put all one's. and for the third row i put all 5's , and here is the great averaged of the 3 output i got. which is what i wanted as soon as i thought up this idea. HAHA.  Array ( [0] => unique [friend] => unique [1] => 2.3333 [AVG(loyalty)] => 2.3333 [2] => 2.3333 [AVG(courtesy)] => 2.3333 [3] => 2.3333 [AVG(stability)] => 2.3333 [4] => 2.3333 [AVG(attitude)] => 2.3333 )   CHECK DAT OUT!!!! sweet!  now i just have to put it into a table, and get the averages for each attribute, like loyalty, etc, because the above average will just give them an average grade based on all the users input for that name. so i want to display the average grade next to the name, which is what the above output is for, then i want to display a grade for each attribute like "loyalty" etc. then the application will be complete! GREAT!!!! 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/2/#findComment-998569 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 ok now its getting complicated, the final stretch, outputting the average grade overall, and outputting the overall attribute grades. lol. im trying to get the average of the arrays elements and output, "unique" has an average grade of "A" or something similar. please help if you have time, thanks. derek here it is (eventually i want to put the output into an html table)  i keep getting this message at the top even though i dont think im echoing the array  Array ( [0] => unique [friend] => unique [1] => 2.3333 [AVG(loyalty)] => 2.3333 [2] => 2.3333 [AVG(courtesy)] => 2.3333 [3] => 2.3333 [AVG(stability)] => 2.3333 [4] => 2.3333 [AVG(attitude)] => 2.3333 )  <?php include("connect1.php"); //////////////////////////////////////// //////////////////////////////////////// // 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,AVG(loyalty), AVG(courtesy), AVG(stability), AVG(attitude) FROM friendgrade WHERE friend IN (SELECT friend FROM friendgrade WHERE friend='$find') GROUP BY friend "; $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 "$field2 has an average grade of <strong>F.</strong> ";   } else if ($final_grade > 1 && $average <= 2) {    echo "$field2 has an average grade of <strong>D.</strong> ";  } else if ($final_grade > 2 && $average <= 3) {     echo "$field2 has an average grade of <strong>C.</strong> ";  } else if ($final_grade > 3 && $average <= 4) {    echo "$field2 has an average grade of <strong>B.</strong> ";  } else if ($final_grade > 4 && $average <= 5) {    echo "$field2 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/2/#findComment-998589 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 crud, i forgot to make it so that the friend search is also based on their zip code. 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/2/#findComment-998595 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 nevermind i think i found the error i will post the results 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/2/#findComment-998604 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 i put for friend "izzy" a grade of A and F, and when i search friends it is supposed to say,  "izzy has an average grade of C"  but it outputs nothing  here is the code if you can help more please. thanks .derek  <?php include("connect1.php"); //////////////////////////////////////// //////////////////////////////////////// // 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,AVG(loyalty), AVG(courtesy), AVG(stability), AVG(attitude) FROM friendgrade WHERE friend IN (SELECT friend FROM friendgrade WHERE friend='$find') GROUP BY friend "; $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/2/#findComment-998616 Share on other sites More sharing options...
kickstart Posted January 20, 2010 Share Posted January 20, 2010 Hi  The SQL:-  SELECT friend,AVG(loyalty), AVG(courtesy), AVG(stability), AVG(attitude) FROM friendgrade WHERE friend IN (SELECT friend FROM friendgrade WHERE friend='$find') GROUP BY friend  originally had name instead of friend in the subselect (ie name='$find'). The idea was that friendgrade would have one row per person per friend. Ie Fred might have 10 rows, one for each of his friends. The subselect would thus bring back a list of all a persons friends, while the outer select would then calculate the average rating of that friend between all people who had that friend.  If you have one row per friend and nothing saying whose friend they are then you may as well just use:-  SELECT friend, loyalty, courtesy, stability, attitude FROM friendgrade WHERE friend = '$find'  That just gives you the row for a friend.  Have you got error reporting turned on now?  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/2/#findComment-998658 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 i was putting an array index in a string. oops. but the page still comes up blank on a friend search. here is what i fixed. i think  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> ";   } 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/2/#findComment-998661 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 great thanks. i dont know how to turn error reporting on. but this is the problem code i think. when i search for the friend. it doesnt give me an average grade, it just shows nothing. thanks for helping again. this is great.  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 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/2/#findComment-998669 Share on other sites More sharing options...
kickstart Posted January 20, 2010 Share Posted January 20, 2010 Hi  There was a link earlier for setting error reporting on.  http://php.net/manual/en/function.error-reporting.php  However the reason you are not getting anything output is the following line:-  $final_grade= $userinfo[1]+$userinfo[2]+$userinfo[3]+$userinfo[4]/4; //compute final grade  Division (ie / ) is done before addition, hence this is adding $userinfo[1]+$userinfo[2]+$userinfo[3] to a quarter of $userinfo[4], which is likely to be far more than 5.  You need to force the order using brackets:-  $final_grade= ($userinfo[1]+$userinfo[2]+$userinfo[3]+$userinfo[4])/4; //compute final grade  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/2/#findComment-998714 Share on other sites More sharing options...
silverglade Posted January 20, 2010 Author Share Posted January 20, 2010 thanks i changed the code, but when i do a search now, on "tom" and tom has a grade of A and F, it just outputs (blank) has a grade of A. and no matter who i search for , its always the same thing,  "has a grade of A". or sometimes "has a grade of F" but it has no relation to the real values i want to average. any more help greatly appreciated. thanks. derek i turned on error reporting and get no php errors.  here is the new code     //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  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/2/#findComment-998728 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.