jasonhardwick Posted May 9, 2008 Share Posted May 9, 2008 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 More sharing options...
rarebit Posted May 9, 2008 Share Posted May 9, 2008 where do you select 'grades'??? Theres so much code, just show whats relevant... Link to comment https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536826 Share on other sites More sharing options...
GingerRobot Posted May 9, 2008 Share Posted May 9, 2008 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 https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536830 Share on other sites More sharing options...
jasonhardwick Posted May 9, 2008 Author Share Posted May 9, 2008 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 More sharing options...
BlueSkyIS Posted May 9, 2008 Share Posted May 9, 2008 SELECT AVG(some_field) FROM TABLE Link to comment https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536851 Share on other sites More sharing options...
jasonhardwick Posted May 9, 2008 Author Share Posted May 9, 2008 I guess I need to call the post number from my "forum_question" table and then open my "forum_answer" table and search that number then average the answer grades... but i have know idea what this would look like. Link to comment https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536908 Share on other sites More sharing options...
jasonhardwick Posted May 9, 2008 Author Share Posted May 9, 2008 Is this any closer? <? $c_avg="SELECT AVG ('a_concept') FROM '$tbl_name2' WHERE question_id='$id'"; echo $c_avg; ?> Link to comment https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536938 Share on other sites More sharing options...
jasonhardwick Posted May 9, 2008 Author Share Posted May 9, 2008 Ok so i'm working with this code now but it doesn't generate anything... <? $c_avg =("SELECT AVG(a_concept) FROM '$tbl_name2' WHERE question_id='$id'"); $c_avg1=mysql_query($c_avg); echo ($c_avg1); ?> Whats not there? Link to comment https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536972 Share on other sites More sharing options...
peranha Posted May 9, 2008 Share Posted May 9, 2008 <?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 More sharing options...
jasonhardwick Posted May 9, 2008 Author Share Posted May 9, 2008 ok i tried that and got this error Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/content/j/a/s/jasonhardwick/html/view_topic.php on line 132 Line 132 is: $query_data1=mysql_fetch_row($c_avg1); Link to comment https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536980 Share on other sites More sharing options...
peranha Posted May 9, 2008 Share Posted May 9, 2008 $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 https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536984 Share on other sites More sharing options...
jasonhardwick Posted May 9, 2008 Author Share Posted May 9, 2008 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 More sharing options...
peranha Posted May 9, 2008 Share Posted May 9, 2008 try this $c_avg =("SELECT AVG(a_concept) FROM $tbl_name2 WHERE question_id='$id'"); Took out the quotes from the table name Link to comment https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-536997 Share on other sites More sharing options...
jasonhardwick Posted May 9, 2008 Author Share Posted May 9, 2008 Worked thanks a lot.. Link to comment https://forums.phpfreaks.com/topic/104892-averaging-values/#findComment-537009 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.