Jump to content

averaging values


Recommended Posts

I'm trying to display an average "grade" of those that have been posted by reviewers, but have no idea where to start.

 

Thanks

 

<?php
		  
include "config.php";

$tbl_name="forum_question";

$id=$_GET['id'];

$sql="SELECT * FROM $tbl_name WHERE id='$id'";

$result=mysql_query($sql);

$rows=mysql_fetch_array($result);

?>
                                </p>
		    <table width="10" border="2" align="center" cellpadding="5" cellspacing="0" bordercolor="#E99979">
                  <tr>
                    <td>
				<?php
$img_path = "file/".$rows ['file'];

$image_t = getimagesize($img_path); 

//edited this
$wh = imageResize($image_t[0],  
$image_t[1], 600);

function imageResize($width, $height, $target) {

if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}

$width = round($width * $percentage);
$height = round($height * $percentage);

return "width=\"$width\" height=\"$height\"";
}

//and this
echo "<img src=\"$img_path\" $wh />";
?>
	 </td>
                  </tr>
                                </table>
		    <p align="center"> </p>
		    <table width="95%" border="0" align="center" cellpadding="10" cellspacing="0">
                  <tr>
                    <td width="10%" rowspan="3" valign="top" bgcolor="#E99979"><b><? echo $rows['username']; ?></b></td>
                    <td height="20" valign="top" bgcolor="#E99979"><strong><? echo $rows['topic']; ?></strong></td>
                    <td valign="top" bgcolor="#E99979"><div align="right">Average Concept Grade: <? echo $rows['a_concept'];?> Average Execution Grade: <? echo $rows['a_execution'];?></div></td>
                  </tr>
                  <tr>
                    <td colspan="2"><? echo $rows['detail']; ?></td>
                  </tr>
                  
                  <tr>
                    <td colspan="2"><div align="right"><hr />Posted: <? echo $rows['datetime']; ?></div></td>
                  </tr>
                                </table>
		    <p>
		      <?php
$tbl_name2="forum_answer"; // Switch to table "forum_answer"

$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";
$result2=mysql_query($sql2);

while($rows=mysql_fetch_array($result2)){
?>

		    </p>
		    <table width="95%" border="0" align="center" cellpadding="10" cellspacing="0">
                  <tr>
                    <td width="10%" rowspan="3" valign="top" bgcolor="#E9CB9E"><b><? echo $rows['a_name']; ?></b></td>
                    <td height="20" valign="top" bgcolor="#E9CB9E"><strong> Review #<? echo $rows['a_id']; ?></strong></td>
                    <td valign="top" bgcolor="#E9CB9E"><div align="right">
                    Concept Grade: <b>
				<?
				$concept = $rows['a_concept'];

				if ($concept == "6") {echo "A+";}
				if ($concept == "5.5") {echo "A";}
				if ($concept == "5") {echo "A-";}
				if ($concept == "4.5") {echo "B+";}
				if ($concept == "4") {echo "B";}
				if ($concept == "3.5") {echo "B-";}
				if ($concept == "3") {echo "C+";}
				if ($concept == "2.5") {echo "C";}
				if ($concept == "2") {echo "C-";}
				if ($concept == "1.5") {echo "D+";}
				if ($concept == "1") {echo "D";}
				if ($concept == ".5") {echo "D-";}

				?> </b>
                    Execution Grade: <b>
				<? 	
				$execution = $rows['a_execution'];

				if ($execution == "6") {echo "A+";}
				if ($execution == "5.5") {echo "A";}
				if ($execution == "5") {echo "A-";}
				if ($execution == "4.5") {echo "B+";}
				if ($execution == "4") {echo "B";}
				if ($execution == "3.5") {echo "B-";}
				if ($execution == "3") {echo "C+";}
				if ($execution == "2.5") {echo "C";}
				if ($execution == "2") {echo "C-";}
				if ($execution == "1.5") {echo "D+";}
				if ($execution == "1") {echo "D";}
				if ($execution == ".5") {echo "D-";}
				?></b>
                    </div></td>
                  </tr>
                  <tr>
                    <td colspan="2"><? echo $rows['a_answer']; ?>
                    <div align="right"></div></td>
                  </tr>
                  <tr>
                    <td colspan="2"><div align="right"><hr />Posted: <? echo $rows['a_datetime']; ?></div></td>
                  </tr>
                                </table>
		    <p><BR>
		      <br>

                  <?
}

$sql3="SELECT view FROM $tbl_name WHERE id='$id'";
$result3=mysql_query($sql3);

$rows=mysql_fetch_array($result3);
$view=$rows['view'];

// if have no counter value set counter = 1
if(empty($view)){
$view=1;
$sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";
$result4=mysql_query($sql4);
}

// count more value
$addview=$view+1;
$sql5="update $tbl_name set view='$addview' WHERE id='$id'";
$result5=mysql_query($sql5);

mysql_close();
?>

                

Link to comment
Share on other sites

More information is required. How are you stroing these grades? Numerically? Or as in a school system (A,B,C etc). If it's the latter, how do you propose to average them? Are you giving each a numerical value?

 

Is the data in a database?

Link to comment
Share on other sites

yes the grades are stored in my database as numeric values IE A+ = 6, F=0

but the grades are stored in the "forum_answer" table so i'm trying to get an average and place it in the original post area.

 

Hope this is clear enough

 

<?
				$concept = $rows['a_concept'];

				if ($concept == "6") {echo "A+";}
				if ($concept == "5.5") {echo "A";}
				if ($concept == "5") {echo "A-";}
				if ($concept == "4.5") {echo "B+";}
				if ($concept == "4") {echo "B";}
				if ($concept == "3.5") {echo "B-";}
				if ($concept == "3") {echo "C+";}
				if ($concept == "2.5") {echo "C";}
				if ($concept == "2") {echo "C-";}
				if ($concept == "1.5") {echo "D+";}
				if ($concept == "1") {echo "D";}
				if ($concept == ".5") {echo "D-";}

				?> </b>
                    Execution Grade: <b>
				<? 	
				$execution = $rows['a_execution'];

				if ($execution == "6") {echo "A+";}
				if ($execution == "5.5") {echo "A";}
				if ($execution == "5") {echo "A-";}
				if ($execution == "4.5") {echo "B+";}
				if ($execution == "4") {echo "B";}
				if ($execution == "3.5") {echo "B-";}
				if ($execution == "3") {echo "C+";}
				if ($execution == "2.5") {echo "C";}
				if ($execution == "2") {echo "C-";}
				if ($execution == "1.5") {echo "D+";}
				if ($execution == "1") {echo "D";}
				if ($execution == ".5") {echo "D-";}
				?>

Link to comment
Share on other sites

<?php
$c_avg =("SELECT AVG(a_concept) FROM '$tbl_name2' WHERE question_id='$id'");
$c_avg1=mysql_query($c_avg);
echo ($c_avg1);

$query_data1 = mysql_fetch_row($c_avg1);

$avgrows = $query_data1[0];

echo "Average: $avgrows";

?>

 

Try that, but always open your php code with <?php not with <?

Link to comment
Share on other sites

$cavg1 = mysql_query($cavg) or die ("Error in query: $query1. ".mysql_error());

 

change the query line to this and see if there is any errors there

 

The code I gave you works fine for me.  and take out the echo ($c_avg1); line

Link to comment
Share on other sites

still no luck so the code:

<?php
$c_avg =("SELECT AVG(a_concept) FROM '$tbl_name2' WHERE question_id='$id'");
$c_avg1 = mysql_query($c_avg) or die ("Error in query: $query1. ".mysql_error());

$query_data1=mysql_fetch_row($c_avg1);

$avgrows = $query_data1[0];

echo "Average: $avgrows";

?>

 

Error:

Error in query: . You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''forum_answer' WHERE question_id='60'' at line 1

 

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.