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
https://forums.phpfreaks.com/topic/104892-averaging-values/
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
https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536839
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
https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536974
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
https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536994
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.