[thelist] php problem: how to sort averages?

Burhan Khalid thelist at meidomus.com
Tue Mar 29 02:57:28 CST 2005


Murat Tutumlu wrote:
> Hi everybody,
> 
> I have a table with "Student_no, lesson, class, V1,
> V2, F1, V3, V4, F2" columns.
> "V1, V2, F1, V3, V4, F2" are numbers over 100. I
> printed averages of classes and it is sorting by class
> name as shown in code below.
> My problem is sorting by averages. I tried, but I
> couldn't.. 
> Any suggestion?
> Thanks in advance..
> 
> <?
> include "config.php";
> $result = mysql_query("SELECT * FROM Studentmarks1
> group by Class ",$db);
> $myrow = mysql_fetch_array($result);
> ?>
> 
> <form action="" method="post" name="form1"
> target="_self">
>   <select name="exam">
>     <option value="">Choose an exam</option>
>     <option value="V1">Visa 1</option>
>     <option value="V2">Visa 2</option>
>     <option value="F1">Final 1</option>
>     <option value="V3">Visa 3</option>
>     <option value="V4">Visa 4</option>
>     <option value="F2">Final 2</option>
>   </select>
> <input type="submit" name="Submit" value="Show">
> </form>
> 
> <?
> do { 
>  
> $sql = mysql_query("SELECT AVG($exam) FROM
> studentmarks1 WHERE class='$myrow[Class]'",$db); 

Change your query thusly:

"SELECT AVG($exam) as exam-avg FROM studentmarks1 WHERE 
class='$myrow[Class]' ORDER BY exam-avg"

Then fetch the average via $row[0], or if you use mysql_fetch_assoc(), 
$row['exam-avg']


More information about the thelist mailing list