Jump to content

incrementing by number a row with a specific user name in a table row


silverglade

Recommended Posts

  • Replies 58
  • Created
  • Last Reply

Top Posters In This Topic

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :D

 

 

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 ) 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!!!!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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> ";

    } 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.